hpcaitech/Open-Sora · error · TypeError

type {type(data)} cannot be converted to tensor.

Error message

type {type(data)} cannot be converted to tensor.

What it means

This TypeError is raised by to_tensor in opensora/utils/misc.py when the input data is not one of the supported types: torch.Tensor, numpy.ndarray, int, float, or a sequence convertible by torch.tensor. The function branches on isinstance checks, and anything else (str, dict, None, arbitrary objects) reaches the else branch. It signals that the caller passed data the conversion utility cannot map to a torch.Tensor.

Source

Thrown at opensora/utils/misc.py:194

        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.

    Returns:
        torch.Tensor: The converted tensor.
    """

    if isinstance(data, torch.Tensor):
        return data
    elif isinstance(data, np.ndarray):
        return torch.from_numpy(data)
    elif isinstance(data, Sequence) and not isinstance(data, str):
        return torch.tensor(data)
    elif isinstance(data, int):
        return torch.LongTensor([data])
    elif isinstance(data, float):
        return torch.FloatTensor([data])
    else:
        raise TypeError(f"type {type(data)} cannot be converted to tensor.")


def to_ndarray(data: torch.Tensor | np.ndarray | Sequence | int | float) -> np.ndarray:
    """Convert objects of various python types to :obj:`numpy.ndarray`.

    Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
    :class:`Sequence`, :class:`int` and :class:`float`.

    Args:
        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.

    Returns:
        numpy.ndarray: The converted ndarray.
    """
    if isinstance(data, torch.Tensor):
        return data.numpy()
    elif isinstance(data, np.ndarray):

View on GitHub (pinned to 7ad6a96a13)

Solutions

  1. Convert the value to a supported type first: np.array(data) or a numeric Python list before calling to_tensor
  2. If the value is text (labels/classes), map it to indices via a vocabulary/label encoder, then convert
  3. Guard with isinstance checks for torch.Tensor, np.ndarray, int, float, or Sequence before calling to_tensor
  4. If None is possible, add an explicit None check and a sensible default

Example fix

# before
t = to_tensor(sample["caption"])

# after
t = to_tensor(label_to_id[sample["label"]])
Defensive patterns

Strategy: type-guard

Validate before calling

import numbers
from collections.abc import Sequence
ok = isinstance(data, (torch.Tensor, np.ndarray, numbers.Integral, numbers.Real, Sequence)) and not isinstance(data, (str, bytes, dict))
if not ok:
    raise TypeError(f"Unsupported input for to_tensor: {type(data)!r}")

Type guard

def is_tensor_convertible(data) -> bool:
    return isinstance(data, (torch.Tensor, np.ndarray, int, float, Sequence)) and not isinstance(data, (str, bytes, dict))

Try / catch

try:
    t = to_tensor(data)
except TypeError:
    t = torch.tensor(as_numeric(data))  # caller-specific numeric coercion

Prevention

When it happens

Trigger: Calling to_tensor("hello"), to_tensor({"a": 1}), to_tensor(None), or to_tensor(some_custom_object). Lists/sequences attempt torch.tensor(data), so non-numeric nested sequences (e.g. ["a", "b"]) also raise, though from torch itself.

Common situations: Feeding string labels or metadata from a dataset/dataloader directly into to_tensor; passing a dict of arrays instead of the arrays themselves; passing None from an optional field that was never populated in a data preprocessing pipeline.

Related errors


AI-assisted analysis of hpcaitech/Open-Sora@7ad6a96a13 (2026-08-28). Data as JSON: /api/errors/7e0f16b10d477439. Report an issue: GitHub.