HumanSignal/label-studio · error · ValidationError

required

required

Error message

This field is required.

What it means

Raised by TaskSerializer.validate when a POST request omits the 'project' field. It uses the standard DRF 'required' field error ('This field is required.') and re-raises it keyed by 'project' so the API response contains a proper per-field error dict.

Source

Thrown at label_studio/tasks/serializers.py:392

            project = generics.get_object_or_404(Project, kwargs['project_id'])
        elif task:
            project = task.project
        else:
            project = None
        return project

    def validate(self, task):
        instance = self.instance if hasattr(self, 'instance') else None

        project = self.project(task=instance)

        current_request = get_current_request()
        if current_request and current_request.method == 'POST' and not project:
            # raise ValidationError for the project field with standard DRF message
            try:
                self.fields['project'].fail('required')
            except ValidationError as exc:
                raise ValidationError(
                    {
                        'project': exc.detail,
                    }
                )

        validator = TaskValidator(
            project,
            instance=instance if 'data' not in task else None,
        )
        return validator.validate(task)

    def create(self, validated_data):
        # Full-overlap projects skip rearrangement on task add (`_update_tasks_states`
        # only rearranges when cohort < 100%). Seed overlap like bulk import / storage
        # sync so is_labeled uses maximum_annotations instead of the model default of 1.
        # Own this here so both POST /api/tasks/ and POST /api/projects/{id}/tasks/ agree.
        project = validated_data.get('project') or self.project()
        if 'overlap' not in validated_data and project is not None and project.overlap_cohort_percentage >= 100:

View on GitHub (pinned to 0b49e9b539)

Solutions

  1. Add the 'project' field (project id) to your POST payload
  2. Prefer the project-scoped endpoint POST /api/projects/<id>/import or /api/projects/<id>/tasks/ so project comes from the URL
  3. If using the Python SDK, pass project=<id> when creating/uploading tasks
  4. Check that your HTTP client/middleware isn't dropping the project field from the JSON body

Example fix

// before
await fetch('/api/tasks/', { method: 'POST', body: JSON.stringify({ tasks: [{ data: { text: 'x' } }] }) });
// after
await fetch('/api/tasks/', { method: 'POST', body: JSON.stringify({ project: 12, tasks: [{ data: { text: 'x' } }] }) });
Defensive patterns

Strategy: validation

Validate before calling

// call project-scoped endpoint instead, or verify project is set
if (!projectId) throw new Error('project id is required for task creation');
await fetch(`/api/projects/${projectId}/import/`, { method: 'POST', body: JSON.stringify(tasks) });

Type guard

function hasProject(payload) {
  return typeof payload === 'object' && payload !== null && Number.isInteger(payload.project);
}

Try / catch

try {
  await api.post('/api/tasks/', payload);
} catch (e) {
  if (e.response?.data?.project?.[0] === 'This field is required.') {
    throw new Error('Add project id to payload or use /api/projects/<id>/import/');
  } throw e;
}

Prevention

When it happens

Trigger: POST to the task-import endpoints (/api/tasks/ or /api/projects/<id>/tasks/ proxies where the project isn't injected into context) with a payload lacking 'project'. The check only fires for POST requests when project is falsy after context lookup.

Common situations: Calling the generic /api/tasks/ endpoint (which requires an explicit project field) instead of the project-scoped /api/projects/<id>/import endpoint; sending tasks via scripts/SDKs without setting project; a proxy or middleware stripping the project key.

Understand the failure class

Background: "Missing required field" and "field is required" errors: why libraries reject payloads that omit mandatory fields — this error's family across 20 libraries.

Related errors


AI-assisted analysis of HumanSignal/label-studio@0b49e9b539 (2026-08-29). Data as JSON: /api/errors/363368426dac9632. Report an issue: GitHub.