sgl-project/sglang · error · TypeError

{name} entries must be tensors or None.

Error message

{name} entries must be tensors or None.

What it means

Thrown by ConditionExpansion._expand_tensors when the value is a list but some entries are neither None nor torch.Tensor — list containers must contain only tensors (or None placeholders).

Source

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

                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 (
                sequence_lengths is None
                or len(sequence_lengths) == self.sample_batch_size
            ):
                expanded.append(sequence_lengths)

View on GitHub (pinned to 0132848349)

Solutions

  1. Use None for missing list entries, not 0 or other defaults
  2. Convert every numpy array entry to a tensor before calling expand_field
  3. Add an assert/isinstance sweep over the list at construction time

Example fix

# before
conds = [t1, 0.0]  # placeholder for missing item
# after
conds = [t1, None]
Defensive patterns

Strategy: type-guard

Validate before calling

assert all(i is None or isinstance(i, torch.Tensor) for i in value), "list entries must be tensors or None"

Type guard

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

Prevention

When it happens

Trigger: Passing a mixed list like [tensor, 0.5] or [np.array(...), tensor] to expand_field.

Common situations: Padding a conditioning list with a default scalar instead of None; partially converted numpy-to-torch lists; inserting a default value for missing items.

Related errors


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