{"record":{"id":"afbef697acf26a54","repo":"sgl-project/sglang","slug":"invalid-grid-metadata-for-kimi-image-tokens-vals","errorCode":null,"errorMessage":"Invalid grid metadata for kimi image tokens: {vals} (expected [t,h,w] or [h,w])","messagePattern":"Invalid grid metadata for kimi image tokens: (.+?) \\(expected \\[t,h,w\\] or \\[h,w\\]\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/multimodal/processors/kimi_common.py","lineNumber":79,"sourceCode":"        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\n    ) -> MultimodalProcessorOutput:\n        image_token_id = kwargs.get(\"image_token_id\", 0)\n        img_grid_thw = kwargs.get(\"img_grid_thw\", None)\n\n        if not isinstance(prompt, list):\n            prompt = self._tokenizer.encode(prompt)\n\n        image_token_counts = [\n            self._num_image_tokens_from_grid(grid) for grid in img_grid_thw","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/multimodal/processors/kimi_common.py#L61-L97","documentation":"After flattening grid_thw, the Kimi helper requires at least 2 values ([h,w] or [t,h,w] with more) to derive patch dimensions. Fewer than 2 values means the grid is malformed (empty or a single scalar), so it cannot compute the image token count and raises ValueError.","triggerScenarios":"Passing an empty tensor/array/list as grid_thw, or a grid with a single element, to _num_image_tokens_from_grid via _build_kimi_mm_data_from_grids or get_mm_data.","commonSituations":"Empty per-image grid lists because the vision encoder returned no output for a corrupt/blank image, or a data pipeline that dropped grid entries during batching/serialization.","solutions":["Validate every image's grid has t*h*w >= 2 entries before calling","Check upstream vision-encoder output for dropped/failed images (corrupt files, zero-byte uploads)","Pad or regenerate missing grid entries from the processor's image_sizes instead of forwarding empty grids"],"exampleFix":"// before\nbuild_from_grids(prompt, grids=[torch.tensor([])])  # one image, empty grid\n\n// after\nassert all(g.numel() >= 2 for g in grids), f\"malformed grid: {grids}\"\nbuild_from_grids(prompt, grids=grids)","handlingStrategy":"validation","validationCode":"assert all(np.asarray(g).size >= 2 for g in grids), \"each grid needs [t,h,w] or [h,w]\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate grid shapes at the edge of your pipeline","Watch for empty encoder outputs from corrupt images","Log image↔grid pairs when loading batches"],"tags":["kimi","multimodal","grid-metadata","validation"],"backgroundTag":"malformed-metadata-shape","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}