{"record":{"id":"781e257ab8f0bb68","repo":"hpcaitech/Open-Sora","slug":"type-type-data-cannot-be-converted-to-ndarray","errorCode":null,"errorMessage":"type {type(data)} cannot be converted to ndarray.","messagePattern":"type (.+?) cannot be converted to ndarray\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"opensora/utils/misc.py","lineNumber":221,"sourceCode":"    Args:\n        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to\n            be converted.\n\n    Returns:\n        numpy.ndarray: The converted ndarray.\n    \"\"\"\n    if isinstance(data, torch.Tensor):\n        return data.numpy()\n    elif isinstance(data, np.ndarray):\n        return data\n    elif isinstance(data, Sequence):\n        return np.array(data)\n    elif isinstance(data, int):\n        return np.ndarray([data], dtype=int)\n    elif isinstance(data, float):\n        return np.array([data], dtype=float)\n    else:\n        raise TypeError(f\"type {type(data)} cannot be converted to ndarray.\")\n\n\ndef to_torch_dtype(dtype: str | torch.dtype) -> torch.dtype:\n    \"\"\"\n    Convert a string or a torch.dtype to a torch.dtype.\n\n    Args:\n        dtype (str | torch.dtype): The input dtype.\n\n    Returns:\n        torch.dtype: The converted dtype.\n    \"\"\"\n    if isinstance(dtype, torch.dtype):\n        return dtype\n    elif isinstance(dtype, str):\n        dtype_mapping = {\n            \"float64\": torch.float64,\n            \"float32\": torch.float32,","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/hpcaitech/Open-Sora/blob/7ad6a96a135feb81f755c84fb391818718f6beb2/opensora/utils/misc.py#L203-L239","documentation":"This TypeError is raised by to_ndarray in opensora/utils/misc.py when the input is not an np.ndarray, torch.Tensor, int, float, or a value np.array/np.ndarray can construct from. It is the numpy counterpart of to_tensor and exists to reject data types the conversion matrix does not cover. Hitting it means the caller passed an unsupported object such as a string, dict, or None.","triggerScenarios":"Calling to_ndarray(\"1.5\"), to_ndarray({\"x\": [1,2]}), to_ndarray(None), or to_ndarray(some_object) where the object is not array-like. Note the int branch uses np.ndarray([data], dtype=int), which itself errors for unusual ints, but the explicit raise fires for anything outside the isinstance chain.","commonSituations":"Processing dataset fields that are strings (file paths, captions) or dicts of lists; passing optional fields that resolved to None; migrating code from to_tensor to to_ndarray with data that was never numeric.","solutions":["Convert the input to an array-like numeric structure first (e.g. list of floats, np.array) before calling to_ndarray","For string data, parse/encode it (float(x) for numeric strings, label encoding for classes) then convert","Pre-validate with isinstance checks for np.ndarray, torch.Tensor, int, float, or Sequence","Handle None explicitly with a default or an early return"],"exampleFix":"# before\narr = to_ndarray(meta[\"fps\"])\n\n# after\narr = to_ndarray(float(meta[\"fps\"]))","handlingStrategy":"type-guard","validationCode":"import numbers\nfrom collections.abc import Sequence\nok = isinstance(data, (np.ndarray, torch.Tensor, numbers.Integral, numbers.Real, Sequence)) and not isinstance(data, (str, bytes, dict))\nif not ok:\n    raise TypeError(f\"Unsupported input for to_ndarray: {type(data)!r}\")","typeGuard":"def is_ndarray_convertible(data) -> bool:\n    return isinstance(data, (np.ndarray, torch.Tensor, int, float, Sequence)) and not isinstance(data, (str, bytes, dict))","tryCatchPattern":"try:\n    arr = to_ndarray(data)\nexcept TypeError:\n    arr = np.asarray(data, dtype=float)","preventionTips":["Parse/encode strings and flatten dicts before calling to_ndarray","Default optional fields so None never reaches the converter"],"tags":["numpy","typeerror","type-conversion","opensora"],"backgroundTag":"numpy-conversion-type-error","analyzedSha":"7ad6a96a135feb81f755c84fb391818718f6beb2","analyzedAt":"2026-08-28T16:58:37.171Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}