{"record":{"id":"1c10f343a06c1e81","repo":"facebookresearch/detectron2","slug":"unsupported-type-for-concatenation","errorCode":null,"errorMessage":"Unsupported type {} for concatenation","messagePattern":"Unsupported type (.+?) for concatenation","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"detectron2/structures/instances.py","lineNumber":182,"sourceCode":"        if len(instance_lists) == 1:\n            return instance_lists[0]\n\n        image_size = instance_lists[0].image_size\n        if not isinstance(image_size, torch.Tensor):  # could be a tensor in tracing\n            for i in instance_lists[1:]:\n                assert i.image_size == image_size\n        ret = Instances(image_size)\n        for k in instance_lists[0]._fields.keys():\n            values = [i.get(k) for i in instance_lists]\n            v0 = values[0]\n            if isinstance(v0, torch.Tensor):\n                values = torch.cat(values, dim=0)\n            elif isinstance(v0, list):\n                values = list(itertools.chain(*values))\n            elif hasattr(type(v0), \"cat\"):\n                values = type(v0).cat(values)\n            else:\n                raise ValueError(\"Unsupported type {} for concatenation\".format(type(v0)))\n            ret.set(k, values)\n        return ret\n\n    def __str__(self) -> str:\n        s = self.__class__.__name__ + \"(\"\n        s += \"num_instances={}, \".format(len(self))\n        s += \"image_height={}, \".format(self._image_size[0])\n        s += \"image_width={}, \".format(self._image_size[1])\n        s += \"fields=[{}])\".format(\", \".join((f\"{k}: {v}\" for k, v in self._fields.items())))\n        return s\n\n    __repr__ = __str__\n","sourceCodeStart":164,"sourceCodeEnd":195,"githubUrl":"https://github.com/facebookresearch/detectron2/blob/a2f4a8771ab77e8411c26b27f24f9489a28a2453/detectron2/structures/instances.py#L164-L195","documentation":"Instances.cat concatenates fields across images: tensors via torch.cat, lists via chaining, and any type exposing a classmethod cat(). A field holding any other type (e.g. dict, str, numpy array) cannot be concatenated and raises ValueError.","triggerScenarios":"Instances.cat([inst_a, inst_b]) where a field holds an unsupported type such as a numpy array or a dict per instance.","commonSituations":"Custom fields added via instances.set('my_metadata', np.array(...)) or python dicts; concatenating per-GPU results in multi-GPU evaluation.","solutions":["Store custom data in a torch.Tensor or a list (both supported)","Give your custom class a classmethod cat(list_of_objs) so type(v).cat works","Drop the unsupported field before calling cat: fields.pop('my_metadata')"],"exampleFix":"# before\ninstances.set('track_ids', np.array([1, 2]))\nmerged = Instances.cat([a, b])\n# after\ninstances.set('track_ids', torch.as_tensor([1, 2]))\nmerged = Instances.cat([a, b])","handlingStrategy":"type-guard","validationCode":"import torch\ndef field_is_concatenable(v) -> bool:\n    return isinstance(v, torch.Tensor) or isinstance(v, list) or hasattr(type(v), 'cat')","typeGuard":"import torch\ndef can_cat_field(v) -> bool:\n    return isinstance(v, (torch.Tensor, list)) or hasattr(type(v), 'cat')","tryCatchPattern":null,"preventionTips":["Store custom fields as tensors or lists","Add a cat() classmethod to custom field types"],"tags":["detectron2","instances","concatenation","type-mismatch"],"backgroundTag":"unsupported-type-for-operation","analyzedSha":"a2f4a8771ab77e8411c26b27f24f9489a28a2453","analyzedAt":"2026-08-27T12:08:21.260Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}