{"record":{"id":"2b85b75b4e69b8cc","repo":"sgl-project/sglang","slug":"unsupported-grid-type-for-kimi-image-tokens-type","errorCode":null,"errorMessage":"Unsupported grid type for kimi image tokens: {type(grid_thw)}","messagePattern":"Unsupported grid type for kimi image tokens: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/multimodal/processors/kimi_common.py","lineNumber":70,"sourceCode":"            ),\n            dtype=np.int64,\n        )\n        return int(np.count_nonzero(token_ids == image_token_id))\n\n    def _num_image_tokens_from_grid(\n        self, grid_thw: Union[torch.Tensor, np.ndarray, list, tuple]\n    ) -> int:\n        \"\"\"Compute Kimi-style image token count from 2D/3D grid metadata.\"\"\"\n        merge_h, merge_w = self.hf_config.vision_config.merge_kernel_size\n\n        if isinstance(grid_thw, torch.Tensor):\n            vals = grid_thw.flatten().tolist()\n        elif isinstance(grid_thw, np.ndarray):\n            vals = grid_thw.reshape(-1).tolist()\n        elif isinstance(grid_thw, (list, tuple)):\n            vals = list(np.array(grid_thw).reshape(-1).tolist())\n        else:\n            raise TypeError(\n                f\"Unsupported grid type for kimi image tokens: {type(grid_thw)}\"\n            )\n\n        if len(vals) >= 3:\n            _t, h, w = vals[-3], vals[-2], vals[-1]\n        elif len(vals) == 2:\n            _t, h, w = 1, vals[0], vals[1]\n        else:\n            raise ValueError(\n                f\"Invalid grid metadata for kimi image tokens: {vals} \"\n                \"(expected [t,h,w] or [h,w])\"\n            )\n\n        h, w = int(h), int(w)\n        return (h * w) // (merge_h * merge_w)\n\n    def _build_kimi_mm_data_from_grids(\n        self, prompt, embeddings, **kwargs","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/multimodal/processors/kimi_common.py#L52-L88","documentation":"Kimi multimodal token-count helper only accepts grid_thw of torch.Tensor, np.ndarray, list, or tuple. Any other type (e.g. a string, dict, or custom object) reaches the else branch and raises TypeError. The grid encodes [t,h,w] patch counts used to compute how many image tokens each placeholder expands to.","triggerScenarios":"Calling _build_kimi_mm_data_from_grids or get_mm_data with img_grid_thw passed as a non-array type such as a JSON string, dict, or nested custom object instead of a tensor/array/list.","commonSituations":"Deserializing grid metadata from JSON without converting back to arrays, passing raw HF processor output that was serialized/round-tripped, or a custom data loader emitting strings.","solutions":["Convert grid_thw to a list/tuple of ints or a torch.Tensor/np.ndarray before calling the API","If it arrives as a JSON string, parse it first: json.loads(...) then pass the list","Normalize grids at the boundary of your data pipeline with np.asarray(grid, dtype=int)"],"exampleFix":"// before\nbuild(grid_thw=\"[[1,4,4]]\")\n\n// after\nimport json\nbuild(grid_thw=json.loads(\"[[1,4,4]]\"))  # or torch.tensor([[1,4,4]])","handlingStrategy":"type-guard","validationCode":"import numpy as np\nassert isinstance(grid_thw, (torch.Tensor, np.ndarray, list, tuple)), type(grid_thw)\ngrid_thw = np.asarray(grid_thw)","typeGuard":"def is_supported_grid(g) -> bool:\n    return isinstance(g, (torch.Tensor, np.ndarray, list, tuple))","tryCatchPattern":"try:\n    build_from_grids(prompt, grids)\nexcept TypeError as e:\n    if \"Unsupported grid type\" in str(e):\n        grids = [np.asarray(json.loads(g)) if isinstance(g, str) else np.asarray(g) for g in grids]\n        build_from_grids(prompt, grids)\n    else:\n        raise","preventionTips":["Normalize grid metadata to tensors/arrays at ingestion","Never pass JSON-serialized grids directly","Add boundary type checks in data loaders"],"tags":["kimi","multimodal","type-validation","grid-metadata"],"backgroundTag":"unsupported-argument-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}