sgl-project/sglang · error · ValueError

Empty multimodal encoder output.

Error message

Empty multimodal encoder output.

What it means

After unwrapping the encoder output (pooler_output, tuple[0]), a list/tuple output of length 0 means no embeddings were produced, which downstream concatenation cannot handle.

Source

Thrown at python/sglang/srt/models/transformers.py:1425

            if value.is_floating_point() and dtype is not None:
                return value.to(dtype=dtype, device=device)
            return value
        if isinstance(value, dict):
            return {k: self._cast_mm_value(v, dtype, device) for k, v in value.items()}
        if isinstance(value, list):
            return [self._cast_mm_value(v, dtype, device) for v in value]
        if isinstance(value, tuple):
            return tuple(self._cast_mm_value(v, dtype, device) for v in value)
        return value

    def _to_tensor_output(self, output) -> torch.Tensor:
        if hasattr(output, "pooler_output") and output.pooler_output is not None:
            output = output.pooler_output
        if isinstance(output, tuple):
            output = output[0]
        if isinstance(output, (list, tuple)):
            if len(output) == 0:
                raise ValueError("Empty multimodal encoder output.")
            if all(torch.is_tensor(x) for x in output):
                output = torch.cat(
                    [x.reshape(-1, x.shape[-1]) if x.ndim > 2 else x for x in output],
                    dim=0,
                )
            else:
                output = output[0]
        elif hasattr(output, "last_hidden_state"):
            output = output.last_hidden_state
        elif isinstance(output, dict):
            if output.get("pooler_output", None) is not None:
                output = output["pooler_output"]
            else:
                output = next(v for v in output.values() if torch.is_tensor(v))
            if isinstance(output, (list, tuple)):
                if len(output) == 0:
                    raise ValueError("Empty multimodal encoder output.")
                if all(torch.is_tensor(x) for x in output):

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure pixel inputs are None (skip encoding) rather than empty when the batch has no items
  2. Guard callers to not invoke the encoder with zero items
  3. Upgrade if a fix handles empty batches gracefully

Example fix

// before
if isinstance(output, (list, tuple)) and len(output) == 0:
    raise ValueError("Empty multimodal encoder output.")
// after
if isinstance(output, (list, tuple)) and len(output) == 0:
    return torch.empty(0, hidden_size, device=..., dtype=...)
Defensive patterns

Strategy: validation

Validate before calling

if items is None or len(items) == 0:
    embeds = None  # skip encoder call
else:
    embeds = model.get_multimodal_embeddings(...)

Try / catch

try:
    out = model._to_tensor_output(enc_out)
except ValueError as e:
    if 'Empty multimodal encoder output' in str(e):
        out = torch.empty(0, hidden_size)
    else: raise

Prevention

When it happens

Trigger: An encoder returning [] (e.g. zero images passed through, filtered batch, or a model returning empty last_hidden_state list) during _encode_modality_items.

Common situations: Empty image batch after filtering; processor returning empty pixel_values but not None; edge-case prompts with only special tokens.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/e6ce68d40714487a. Report an issue: GitHub.