sgl-project/sglang · error · TypeError

{name} must be a tensor, list of tensors, or None.

Error message

{name} must be a tensor, list of tensors, or None.

What it means

Thrown by ConditionExpansion._expand_tensors when the field value is not None, not a torch.Tensor, and not a list — only tensors, lists of tensors (entries may be None), and None are expandable conditioning values.

Source

Thrown at python/sglang/multimodal_gen/runtime/utils/condition_expansion.py:53

        if current_batch_size == self.sample_batch_size:
            return value
        if current_batch_size != self.prompt_batch_size:
            raise ValueError(
                f"{name} has batch dim {current_batch_size} (shape "
                f"{tuple(value.shape)}); expected {self.prompt_batch_size} "
                f"(per-prompt) or {self.sample_batch_size} (per-sample)."
            )
        repeats = self.sample_batch_size // self.prompt_batch_size
        return value.repeat_interleave(repeats, dim=0)

    def _expand_tensors(self, value, name: str):
        """Expand a tensor or each tensor in a list, preserving its container."""
        if value is None:
            return None
        if isinstance(value, torch.Tensor):
            return self._expand_tensor(value, name)
        if not isinstance(value, list):
            raise TypeError(f"{name} must be a tensor, list of tensors, or None.")
        if any(
            item is not None and not isinstance(item, torch.Tensor) for item in value
        ):
            raise TypeError(f"{name} entries must be tensors or None.")
        return [
            self._expand_tensor(item, f"{name}[{index}]") if item is not None else None
            for index, item in enumerate(value)
        ]

    def _expand_sequence_lengths(
        self, value: list[list[int] | None] | None, name: str
    ) -> list[list[int] | None] | None:
        if value is None:
            return None
        repeats = self.sample_batch_size // self.prompt_batch_size
        expanded = []
        for index, sequence_lengths in enumerate(value):
            if (

View on GitHub (pinned to 0132848349)

Solutions

  1. Convert the value to a torch.Tensor first: torch.as_tensor(value)
  2. Keep scalars/non-tensor metadata out of expand_field; handle them separately
  3. If using numpy arrays anywhere in conditioning, add an explicit torch.from_numpy conversion at the boundary

Example fix

# before
expand.expand_field(np_array, "condition")
# after
expand.expand_field(torch.from_numpy(np_array), "condition")
Defensive patterns

Strategy: type-guard

Validate before calling

import torch
def is_expandable(v) -> bool:
    return v is None or isinstance(v, torch.Tensor) or (isinstance(v, list) and all(i is None or isinstance(i, torch.Tensor) for i in v))

Type guard

def is_expandable(v) -> bool:
    import torch
    return v is None or isinstance(v, torch.Tensor) or (isinstance(v, list) and all(i is None or isinstance(i, torch.Tensor) for i in v))

Try / catch

try:
    out = exp.expand_field(value, name)
except TypeError:
    out = exp.expand_field(torch.as_tensor(value), name)

Prevention

When it happens

Trigger: Passing a float, numpy array, string, or dict to expand_field — e.g. a numpy conditioning array or a scalar guidance scale.

Common situations: Numpy-based pipelines feeding np.ndarray conditioning; scalar per-batch hyperparameters mistakenly routed through expand_field.

Related errors


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