{"record":{"id":"e5968949d86fda79","repo":"sgl-project/sglang","slug":"kimi-k3-image-feature-must-be-a-torch-tensor-got","errorCode":null,"errorMessage":"Kimi-K3 image feature must be a torch.Tensor, got {type(item.feature)}","messagePattern":"Kimi-K3 image feature must be a torch\\.Tensor, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/models/kimi_k3.py","lineNumber":3424,"sourceCode":"                        \"Kimi-K3 cannot mix local preprocessed and deferred images\"\n                    )\n                return materialize_multimodal_features(\n                    [item.feature for item in selected_items],\n                    device=device,\n                    dtype=target_dtype,\n                )\n\n            deferred = [\n                item.model_specific_data.get(DEFERRED_PREPROCESSING_KEY)\n                for item in selected_items\n            ]\n            if any(config is not None for config in deferred):\n                materialized = [None] * len(selected_items)\n                deferred_by_backend = {}\n                for index, (item, config) in enumerate(zip(selected_items, deferred)):\n                    if config is None:\n                        if not isinstance(item.feature, torch.Tensor):\n                            raise TypeError(\n                                \"Kimi-K3 image feature must be a torch.Tensor, \"\n                                f\"got {type(item.feature)}\"\n                            )\n                        materialized[index] = item.feature\n                    else:\n                        deferred_by_backend.setdefault(config.backend, []).append(index)\n\n                for backend, indices in deferred_by_backend.items():\n                    group_items = [selected_items[index] for index in indices]\n                    group_configs = [deferred[index] for index in indices]\n                    # Map backend-group positions through the rank-local shard to global grid rows.\n                    global_indices = [image_indices[index] for index in indices]\n                    first_config = group_configs[0]\n                    if backend == \"gpu\":\n                        from sglang.srt.multimodal.processors.kimi_k25 import (\n                            _gpu_preprocess_images,\n                        )\n","sourceCodeStart":3406,"sourceCodeEnd":3442,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/models/kimi_k3.py#L3406-L3442","documentation":"In materialize_item_features(), items without a deferred config must already hold a torch.Tensor in item.feature; any other type raises TypeError. This guards the local path where features are assumed precomputed before materialization.","triggerScenarios":"A MultimodalDataItem with config None (not deferred) whose .feature is not a torch.Tensor — e.g. a numpy array, PIL image, raw pixel list, or None because preprocessing never ran.","commonSituations":"Skipping the local preprocessing step for some items; a pipeline change that stores raw pixels instead of tensor features; None feature from a failed earlier stage.","solutions":["Ensure local items have their .feature set to a torch.Tensor before calling get_image_feature()","Convert non-tensor features with torch.as_tensor(..., dtype=target_dtype) beforehand","If features are not computed, mark items as deferred with a proper backend config"],"exampleFix":"// before\nitem.feature = np_array  # then model.get_image_feature([item])\n\n// after\nitem.feature = torch.from_numpy(np_array)\nmodel.get_image_feature([item])","handlingStrategy":"type-guard","validationCode":"for i in items:\n    if deferred_config(i) is None and not isinstance(i.feature, torch.Tensor):\n        i.feature = torch.as_tensor(i.feature)","typeGuard":"import torch\ndef features_are_tensors(items) -> bool:\n    return all(isinstance(i.feature, torch.Tensor) for i in items if i.feature is not None)","tryCatchPattern":"try:\n    model.get_image_feature(items)\nexcept TypeError as e:\n    if \"torch.Tensor\" in str(e):\n        coerce_features_to_tensors(items)\n        return model.get_image_feature(items)\n    raise","preventionTips":["Always store torch.Tensor in item.feature after preprocessing","Assert feature types in unit tests for your multimodal pipeline"],"tags":["kimi-k3","type-error","feature-tensor","multimodal"],"backgroundTag":"wrong-feature-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}