{"record":{"id":"86e341126b7a852c","repo":"invoke-ai/InvokeAI","slug":"all-items-in-a-batch-must-have-the-same-type","errorCode":null,"errorMessage":"All items in a batch must have the same type","messagePattern":"All items in a batch must have the same type","errorType":"validation","errorClass":"BatchItemsTypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/services/session_queue/session_queue_common.py","lineNumber":123,"sourceCode":"    @field_validator(\"data\")\n    def validate_types(cls, v: Optional[BatchDataCollection]):\n        if v is None:\n            return v\n        for batch_data_list in v:\n            for datum in batch_data_list:\n                if not datum.items:\n                    continue\n\n                # Special handling for numbers - they can be mixed\n                # TODO(psyche): Update BatchDatum to have a `type` field to specify the type of the items, then we can have strict float and int fields\n                if all(isinstance(item, (int, float)) for item in datum.items):\n                    continue\n\n                # Get the type of the first item in the list\n                first_item_type = type(datum.items[0])\n                for item in datum.items:\n                    if type(item) is not first_item_type:\n                        raise BatchItemsTypeError(\"All items in a batch must have the same type\")\n        return v\n\n    @field_validator(\"data\")\n    def validate_unique_field_mappings(cls, v: Optional[BatchDataCollection]):\n        if v is None:\n            return v\n        paths: set[tuple[str, str]] = set()\n        for batch_data_list in v:\n            for datum in batch_data_list:\n                pair = (datum.node_path, datum.field_name)\n                if pair in paths:\n                    raise BatchDuplicateNodeFieldError(\"Each batch data must have unique node_id and field_name\")\n                paths.add(pair)\n        return v\n\n    @model_validator(mode=\"after\")\n    def validate_batch_nodes_and_edges(self):\n        if self.data is None:","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/session_queue/session_queue_common.py#L105-L141","documentation":"All items within a single batch datum's item list must share the same Python/Pydantic type, since batch items are substituted into one node field. The BatchItemsTypeError is raised by validate_types when an item's type differs from the first item's type in the list.","triggerScenarios":"Mixing item types within one batch data list, e.g. string prompts alongside ImageField objects, or ints mixed with floats/strings in the same collection, when the Batch model is validated.","commonSituations":"Programmatically building batch data from heterogeneous inputs without normalizing types; loading batch JSON where one entry was edited to a different type; API clients sending mixed-type arrays.","solutions":["Normalize all items in each batch data list to one type (e.g. convert everything to str, or all to ImageField)","Split heterogeneous data into separate batch data collections","Add client-side validation that checks isinstance(item, type(items[0])) for all items before submission","Fix JSON payloads where a value was accidentally given as a different JSON type"],"exampleFix":"// before: mixed types\nitems: [\"prompt\", 42]\n// after: homogeneous\nitems: [\"prompt one\", \"prompt two\"]","handlingStrategy":"validation","validationCode":"def validate_item_types(batch_data_list):\n    for datum in batch_data_list:\n        if datum.items:\n            first = type(datum.items[0])\n            if any(type(i) is not first for i in datum.items):\n                raise ValueError(f\"Mixed item types in field {datum.field_name}.\")","typeGuard":"def homogeneous(items) -> bool:\n    return len({type(i) for i in items}) <= 1","tryCatchPattern":"try:\n    batch = Batch(**batch_dict)\nexcept BatchItemsTypeError as e:\n    logger.error(f\"Heterogeneous batch items: {e}\")\n    # normalize item types and rebuild","preventionTips":["Cast all items to one type when building batch data programmatically (e.g. str(x))","Never mix JSON scalar types in one items array","Split heterogeneous data into separate batch data entries","Add a type check in scripts that append batch items"],"tags":["batch","validation","types"],"backgroundTag":"batch-item-type-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}