sgl-project/sglang · error · ValueError
Invalid grid metadata for kimi image tokens: {vals} (expecte
Error message
Invalid grid metadata for kimi image tokens: {vals} (expected [t,h,w] or [h,w]) What it means
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.
Source
Thrown at python/sglang/srt/multimodal/processors/kimi_common.py:79
merge_h, merge_w = self.hf_config.vision_config.merge_kernel_size
if isinstance(grid_thw, torch.Tensor):
vals = grid_thw.flatten().tolist()
elif isinstance(grid_thw, np.ndarray):
vals = grid_thw.reshape(-1).tolist()
elif isinstance(grid_thw, (list, tuple)):
vals = list(np.array(grid_thw).reshape(-1).tolist())
else:
raise TypeError(
f"Unsupported grid type for kimi image tokens: {type(grid_thw)}"
)
if len(vals) >= 3:
_t, h, w = vals[-3], vals[-2], vals[-1]
elif len(vals) == 2:
_t, h, w = 1, vals[0], vals[1]
else:
raise ValueError(
f"Invalid grid metadata for kimi image tokens: {vals} "
"(expected [t,h,w] or [h,w])"
)
h, w = int(h), int(w)
return (h * w) // (merge_h * merge_w)
def _build_kimi_mm_data_from_grids(
self, prompt, embeddings, **kwargs
) -> MultimodalProcessorOutput:
image_token_id = kwargs.get("image_token_id", 0)
img_grid_thw = kwargs.get("img_grid_thw", None)
if not isinstance(prompt, list):
prompt = self._tokenizer.encode(prompt)
image_token_counts = [
self._num_image_tokens_from_grid(grid) for grid in img_grid_thwView on GitHub (pinned to 0132848349)
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
Example fix
// before
build_from_grids(prompt, grids=[torch.tensor([])]) # one image, empty grid
// after
assert all(g.numel() >= 2 for g in grids), f"malformed grid: {grids}"
build_from_grids(prompt, grids=grids) Defensive patterns
Strategy: validation
Validate before calling
assert all(np.asarray(g).size >= 2 for g in grids), "each grid needs [t,h,w] or [h,w]"
Prevention
- Validate grid shapes at the edge of your pipeline
- Watch for empty encoder outputs from corrupt images
- Log image↔grid pairs when loading batches
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Grid dim ({_mm_grid_attrs[modality]}) not found in {mm_input
- Invalid Kimi image grid metadata: {values}; expected [h, w]
- Unsupported grid type for kimi image tokens: {type(grid_thw)
- The number of image placeholders exceeds img_grid_thw entrie
- The number of image placeholders does not match img_grid_thw
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/afbef697acf26a54.
Report an issue: GitHub.