sgl-project/sglang · error · TypeError

{field_name} must be a tensor, list of tensors, list of sequ

Error message

{field_name} must be a tensor, list of tensors, list of sequence-length lists, or None.

What it means

expand_field only accepts four shapes for a conditioning field: a single tensor, a list of tensors, a list of sequence-length lists (each item None or a list), or None. Anything else (e.g. a list mixing tensors and ints, a numpy array, a list of dicts) raises this TypeError before setattr on the batch.

Source

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

                )
        return expanded

    def expand_field(self, batch, field_name: str) -> None:
        """Expand one field in place, dispatching from its value type."""
        value = getattr(batch, field_name)
        if value is None:
            return
        if isinstance(value, torch.Tensor) or (
            isinstance(value, list)
            and all(item is None or isinstance(item, torch.Tensor) for item in value)
        ):
            expanded = self._expand_tensors(value, field_name)
        elif isinstance(value, list) and all(
            item is None or isinstance(item, list) for item in value
        ):
            expanded = self._expand_sequence_lengths(value, field_name)
        else:
            raise TypeError(
                f"{field_name} must be a tensor, list of tensors, "
                "list of sequence-length lists, or None."
            )
        setattr(batch, field_name, expanded)

View on GitHub (pinned to 0132848349)

Solutions

  1. Convert numpy arrays to torch tensors before calling expand_field
  2. Wrap bare scalar/int lists as list-of-lists if they represent sequence lengths, or as tensors otherwise
  3. Ensure list fields are homogeneous: all tensors, or all None/list items

Example fix

# before
batch.cond_embeds = np.array([...])
# after
import torch
batch.cond_embeds = torch.from_numpy(np.array([...]))
Defensive patterns

Strategy: type-guard

Validate before calling

import torch
ok = value is None or isinstance(value, torch.Tensor) or (isinstance(value, list) and (all(isinstance(i, torch.Tensor) for i in value) or all(i is None or isinstance(i, list) for i in value)))

Type guard

def is_expandable(value) -> bool:
    if value is None or isinstance(value, torch.Tensor):
        return True
    if isinstance(value, list) and value:
        return all(isinstance(i, torch.Tensor) for i in value) or all(
            i is None or isinstance(i, list) for i in value
        )
    return False

Try / catch

try:
    expand_field(...)
except TypeError as e:
    raise ValueError(f'Bad conditioning field type: {e}') from e

Prevention

When it happens

Trigger: Calling expand_conditioning_to_sample_batch / expand_field with a field value that is a numpy array, a list of ints/floats, or a heterogeneous list (tensors mixed with non-list scalars). Note a plain list of ints fails the `all(item is None or isinstance(item, list))` check.

Common situations: Passing numpy arrays instead of torch tensors; passing raw token-id lists instead of wrapping them in tensors or lists-of-lists; optional fields that become [None, tensor] mixed lists.

Related errors


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