{"record":{"id":"240e5635cdd5038a","repo":"HumanSignal/label-studio","slug":"task-item-should-be-dict","errorCode":null,"errorMessage":"Task item should be dict","messagePattern":"Task item should be dict","errorType":"validation","errorClass":"ValidationError","httpStatus":400,"severity":"error","filePath":"label_studio/data_import/models.py","lineNumber":166,"sourceCode":"        return tasks\n\n    def read_tasks_list_from_json(self):\n        logger.debug('Read tasks list from JSON file {}'.format(self.filepath))\n\n        raw_data = self.content\n        # Python 3.5 compatibility fix https://docs.python.org/3/whatsnew/3.6.html#json\n        try:\n            tasks = json.loads(raw_data)\n        except TypeError:\n            tasks = json.loads(raw_data.decode('utf8'))\n        if isinstance(tasks, dict):\n            tasks = [tasks]\n        tasks_formatted = []\n        for i, task in enumerate(tasks):\n            if not task.get('data'):\n                task = {'data': task}\n            if not isinstance(task['data'], dict):\n                raise ValidationError('Task item should be dict')\n            tasks_formatted.append(task)\n        return tasks_formatted\n\n    def read_tasks_list_from_json_streaming(self, batch_size=100):\n        logger.debug('Read tasks list from JSON file streaming {}'.format(self.filepath))\n\n        try:\n            with self.file.open('rb') as file_handle:\n                # Peek a small prefix to detect top-level container ('[' array or '{' object)\n                sniff = file_handle.read(4096) or b''\n                # Find first non-whitespace byte\n                first_byte = None\n                for b in sniff:\n                    if b not in (0x20, 0x09, 0x0A, 0x0D):  # space, tab, lf, cr\n                        first_byte = b\n                        break\n\n                # Rewind after sniffing","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/data_import/models.py#L148-L184","documentation":"Label Studio's read_tasks_list_from_json normalizes each imported JSON task so that it has a 'data' key holding a dict. When a task's resolved 'data' payload is not a dict (e.g. a string or number), it raises this ValidationError because tasks must map column keys to values.","triggerScenarios":"Calling read_tasks (via FileUpload.read_tasks or load_tasks_from_uploaded_files) with a .json file whose top-level array (or object) yields elements whose data is a non-dict, e.g. a JSON file containing [\"a\",\"b\"] where each bare string becomes data.","commonSituations":"Uploading a JSON file of plain strings or numbers instead of objects; exporting from another tool into a flat JSON array of scalars; hand-edited JSON where the object wrapper was lost.","solutions":["Wrap each item as an object with a $undefined_key or your data key, e.g. [{\"data\": {\"text\": \"a\"}}]","Re-export the file so each element is a JSON object with key/value fields","Validate the JSON structure before upload (every element is an object)"],"exampleFix":"// before (tasks.json)\n[\"first text\", \"second text\"]\n// after\n[{\"data\": {\"text\": \"first text\"}}, {\"data\": {\"text\": \"second text\"}}]","handlingStrategy":"validation","validationCode":"import json\nwith open('tasks.json') as f:\n    tasks = json.load(f)\nif isinstance(tasks, dict):\n    tasks = [tasks]\nfor t in tasks:\n    data = t.get('data') if isinstance(t, dict) else t\n    if not isinstance(data, dict):\n        raise ValueError(f\"Task data must be a dict, got {type(data).__name__}\")","typeGuard":"def is_valid_task(item):\n    if isinstance(item, dict):\n        return isinstance(item.get('data'), dict)\n    return isinstance(item, dict)\nvalid = [t for t in tasks if is_valid_task(t)]","tryCatchPattern":"from rest_framework.exceptions import ValidationError\ntry:\n    tasks = fu.read_tasks()\nexcept ValidationError as e:\n    logger.error(\"Task structure invalid: %s\", e)\n    # fix file or skip","preventionTips":["Export tasks as JSON objects with explicit data keys","Validate JSON structure with a schema before import","Never upload arrays of scalars as .json; use .txt instead"],"tags":["json","validation","data-import"],"backgroundTag":"json-schema-validation-failed","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}