{"record":{"id":"30dac10b1bebccf4","repo":"pola-rs/polars","slug":"invalid-return-type-return-type-r-expected-on-30dac1","errorCode":null,"errorMessage":"invalid `return_type`: {return_type!r}\nExpected one of: {valid_torch_types}","messagePattern":"invalid `return_type`: (.+?)\nExpected one of: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":2531,"sourceCode":"                # return a {\"label\": tensor(s), \"features\": tensor(s)} dict\n                return {\n                    \"label\": label_frame.to_torch(),\n                    \"features\": features_frame.to_torch(),\n                }\n            else:\n                # return a {\"col\": tensor} dict\n                return {srs.name: srs.to_torch() for srs in frame}\n\n        elif return_type == \"dataset\":\n            # return a torch Dataset object\n            from polars.ml.torch import PolarsDataset\n\n            pds_label = None if label_frame is None else label_frame.columns\n            return PolarsDataset(frame, label=pds_label, features=features)\n        else:\n            valid_torch_types = \", \".join(get_args(TorchExportType))\n            msg = f\"invalid `return_type`: {return_type!r}\\nExpected one of: {valid_torch_types}\"\n            raise ValueError(msg)\n\n    def to_pandas(\n        self,\n        *,\n        use_pyarrow_extension_array: bool = False,\n        **kwargs: Any,\n    ) -> pd.DataFrame:\n        \"\"\"\n        Convert this DataFrame to a pandas DataFrame.\n\n        This operation copies data if `use_pyarrow_extension_array` is not enabled.\n\n        Parameters\n        ----------\n        use_pyarrow_extension_array\n            Use PyArrow-backed extension arrays instead of NumPy arrays for the columns\n            of the pandas DataFrame. This allows zero copy operations and preservation\n            of null values. Subsequent operations on the resulting pandas DataFrame may","sourceCodeStart":2513,"sourceCodeEnd":2549,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L2513-L2549","documentation":"Raised by DataFrame.to_torch when `return_type` is not one of TorchExportType = Literal['tensor', 'dataset', 'dict']. Runtime strings are not checked by Literal typing, so anything else (typos, jax vocabulary, config values) falls through the dispatch chain to this final ValueError, which lists the valid options.","triggerScenarios":"`df.to_torch('tenser')`, `df.to_torch('array')` (jax vocabulary), `df.to_torch('Tensor')` (case mismatch), or return_type loaded from a config/CLI that isn't exactly one of the three literals.","commonSituations":"Parameterized export helpers where the backend string comes from YAML; mixed jax/torch codebases sharing a RETURN_TYPE constant; typos and casing errors in notebook code.","solutions":["Use one of 'tensor' (default), 'dataset', or 'dict'","Validate external strings up front against ('tensor', 'dataset', 'dict')","For jax exports use `df.to_jax(...)` with its own valid types"],"exampleFix":"# before\nt = df.to_torch('tenser')\n\n# after\nt = df.to_torch('tensor')","handlingStrategy":"validation","validationCode":"VALID_TORCH = ('tensor', 'dataset', 'dict')\nif return_type not in VALID_TORCH:\n    raise ValueError(f'return_type must be one of {VALID_TORCH}, got {return_type!r}')\nout = df.to_torch(return_type)","typeGuard":"def is_torch_return_type(rt: object) -> bool:\n    \"\"\"to_torch only accepts 'tensor', 'dataset', or 'dict'.\"\"\"\n    return rt in ('tensor', 'dataset', 'dict')","tryCatchPattern":"try:\n    out = df.to_torch(return_type)\nexcept ValueError as e:\n    if 'invalid `return_type`' in str(e):\n        out = df.to_torch('tensor')\n    else:\n        raise","preventionTips":["Whitelist config strings against ('tensor', 'dataset', 'dict') before calling","Use Literal-typed wrappers so static checkers catch typos","Route jax-flavored values like 'array' to df.to_jax instead"],"tags":["torch","export","invalid-argument","ml"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}