{"record":{"id":"0d284de271bddf1b","repo":"pola-rs/polars","slug":"cannot-convert-list-dtype-to-tensor-use-array-dty","errorCode":null,"errorMessage":"cannot convert List dtype to Tensor (use Array dtype instead)","messagePattern":"cannot convert List dtype to Tensor \\(use Array dtype instead\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":4985,"sourceCode":"        torch = import_optional(\"torch\")\n\n        # PyTorch tensors do not support uint16/32/64\n        if self.dtype in (UInt32, UInt64):\n            srs = self.cast(Int64)\n        elif self.dtype == UInt16:\n            srs = self.cast(Int32)\n        else:\n            srs = self\n\n        # we have to build the tensor from a writable array or PyTorch will complain\n        # about it (writing to a readonly array results in undefined behavior)\n        numpy_array = srs.to_numpy(writable=True)\n        try:\n            tensor = torch.from_numpy(numpy_array)\n        except TypeError:\n            if self.dtype == List:\n                msg = \"cannot convert List dtype to Tensor (use Array dtype instead)\"\n                raise TypeError(msg) from None\n            raise\n        # note: named tensors are currently experimental\n        # tensor.rename(self.name)\n        return tensor\n\n    @removed_parameters(\n        RenamedParameter(\n            name=\"future\",\n            new_name=\"compat_level\",\n            deprecated_in=\"1.1\",\n            removed_in=\"2.0\",\n        ),\n    )\n    def to_arrow(self, *, compat_level: CompatLevel | None = None) -> pa.Array:\n        \"\"\"\n        Return the underlying Arrow array.\n\n        If the Series contains only a single chunk this operation is zero copy.","sourceCodeStart":4967,"sourceCodeEnd":5003,"githubUrl":"https://github.com/pola-rs/polars/blob/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/series/series.py#L4967-L5003","documentation":"to_torch conversion guard: PyTorch tensors need fixed-shape elements, so a List-dtype Series cannot be converted; the guard directs users to the fixed-width Array dtype. Raised before any tensor construction.","triggerScenarios":"Thrown at py-polars/src/polars/series/series.py:5145 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Convert the column to the fixed-width Array dtype (e.g. `cast(pl.Array(inner, size))`) before converting to a tensor."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}