sgl-project/sglang · error · TypeError

Input 'data' must be a torch.Tensor, but got {type(data)}

Error message

Input 'data' must be a torch.Tensor, but got {type(data)}

What it means

A Tensor-subclass __new__ (used for SGLang's MM tensor wrapper) requires the 'data' argument to already be a torch.Tensor; it reclasses the tensor rather than constructing one, so lists/ndarrays are type errors.

Source

Thrown at python/sglang/srt/managers/mm_utils.py:132

class TransportProxyTensor(torch.Tensor):
    """
    A convenient torch.Tensor subclass that carries extra metadata and supports
    efficient inter-process communications
    """

    @staticmethod
    def __new__(
        cls,
        data: torch.Tensor,
        name: Optional[str] = None,
        fields: Optional[Dict[str, Any]] = None,
        transport_mode: TensorTransportMode = "default",
        *args,
        **kwargs,
    ):

        if not isinstance(data, torch.Tensor):
            raise TypeError(
                f"Input 'data' must be a torch.Tensor, but got {type(data)}"
            )

        instance = data.as_subclass(cls)

        instance._metadata = {
            "name": name,
            "fields": fields if fields is not None else {},
            "transport_mode": transport_mode,
        }

        return instance

    def __getstate__(self):
        """
        Called during pickling. Implements the serialization logic.
        """
        # acquire all serialize metadata from _metadata

View on GitHub (pinned to 0132848349)

Solutions

  1. Wrap data first: torch.as_tensor(data) or torch.from_numpy(arr)
  2. Check isinstance(data, torch.Tensor) before constructing

Example fix

// before
t = mm_tensor_cls(numpy_array)
// after
t = mm_tensor_cls(torch.from_numpy(numpy_array))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(data, torch.Tensor):
    data = torch.as_tensor(data)

Type guard

def is_tensor(x): return isinstance(x, torch.Tensor)

Try / catch

try:
    t = WrappedTensor(data)
except TypeError:
    t = WrappedTensor(torch.as_tensor(data))

Prevention

When it happens

Trigger: Constructing the wrapper with a numpy array or nested list, e.g. cls(np.array(...)) or cls([[1,2]]) instead of cls(torch.tensor(...)).

Common situations: Porting code from numpy pipelines into the MM utils path; test fixtures passing raw arrays.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


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