HumanSignal/label-studio · error · ValidationError

{item} contains invalid "task" field: corresponding task ID

Error message

{item} contains invalid "task" field: corresponding task ID couldn't be retrieved from project {project} tasks

What it means

In _create_legacy (the legacy prediction creation path), if a prediction item's task ID cannot be resolved to a real task in the project — outside the code path that appends to validation_errors — it raises this ValidationError directly. It is the legacy hard-fail for unresolvable 'task' references.

Source

Thrown at label_studio/data_import/api.py:622

        should_validate_label_config = project.label_config_is_not_default
        li = LabelInterface(project.label_config) if should_validate_label_config else None

        # Validate all predictions before creating any
        validation_errors = []
        predictions = []

        for i, item in enumerate(self.request.data):
            item = sanitize_prediction_import_payload(item)
            # Validate task ID
            if item.get('task') not in tasks_ids:
                if flag_set('fflag_feat_utc_210_prediction_validation_15082025', user='auto'):
                    validation_errors.append(
                        f'Prediction {i}: Invalid task ID {item.get("task")} - task not found in project'
                    )
                    continue
                else:
                    # Before change we raised only here
                    raise ValidationError(
                        f'{item} contains invalid "task" field: corresponding task ID couldn\'t be retrieved '
                        f'from project {project} tasks'
                    )

            if should_validate_label_config:
                try:
                    validation_errors_list = li.validate_prediction(item, return_errors=True)

                    # If prediction is invalid, add error to validation_errors list and continue to next prediction
                    if validation_errors_list:
                        # Format errors for better readability
                        for error in validation_errors_list:
                            validation_errors.append(f'Prediction {i}: {error}')
                        continue

                except Exception as e:
                    validation_errors.append(f'Prediction {i}: Error validating prediction - {extract_message(e)}')
                    continue

View on GitHub (pinned to 0b49e9b539)

Solutions

  1. Confirm the 'task' ID exists in the project before creating the prediction (GET /api/tasks/{id}/)
  2. Re-pair predictions with their tasks by re-exporting from the source project
  3. Remove predictions referencing deleted/nonexistent tasks from the payload
  4. Upgrade to a version with the batched validation path, which reports all errors instead of failing on the first

Example fix

// before
tasks = {t['id'] for t in project_tasks}
predictions = [p for p in exported if p['task'] not in tasks]  # includes stale IDs
// after
predictions = [p for p in exported if p['task'] in tasks]  # only resolvable references
Defensive patterns

Strategy: validation

Validate before calling

project_tasks = {t['id'] for t in paginate(f'{LS}/api/projects/{pid}/tasks')}
for p in predictions:
    if p.get('task') not in project_tasks:
        raise ValueError(f'task {p.get("task")!r} not in project {pid}')

Type guard

def resolvable(p: dict, task_ids: set) -> bool:
    return isinstance(p.get('task'), int) and p['task'] in task_ids

Try / catch

try:
    requests.post(import_url, headers=H, json=payload).raise_for_status()
except requests.HTTPError as e:
    if 'corresponding task ID' in e.response.text:
        raise SystemExit('Payload contains task references absent from this project; re-export with tasks')

Prevention

When it happens

Trigger: Creating predictions with an item whose 'task' ID does not match any task in the project and hits the else branch of the task lookup (e.g. lookup returned falsy in a way not captured by the earlier validation_errors path), via the predictions/import create endpoint.

Common situations: Same as foreign-key mismatches: cross-project or cross-instance task IDs, deleted tasks, missing 'task' key; hitting legacy code path on older deployments where the softer validation_errors accumulation is not in effect.

Related errors


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