{"record":{"id":"be1f06c2217fe89b","repo":"pola-rs/polars","slug":"invalid-return-type-return-type-r-expected-on","errorCode":null,"errorMessage":"invalid `return_type`: {return_type!r}\nExpected one of: {valid_jax_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":2291,"sourceCode":"                if label is not None:\n                    # return a {\"label\": array(s), \"features\": array(s)} dict\n                    label_frame = frame.select(label)\n                    features_frame = (\n                        frame.select(features)\n                        if features is not None\n                        else frame.drop(*label_frame.columns)\n                    )\n                    return {\n                        \"label\": label_frame.to_jax(),\n                        \"features\": features_frame.to_jax(),\n                    }\n                else:\n                    # return a {\"col\": array} dict\n                    return {srs.name: srs.to_jax() for srs in frame}\n            else:\n                valid_jax_types = \", \".join(get_args(JaxExportType))\n                msg = f\"invalid `return_type`: {return_type!r}\\nExpected one of: {valid_jax_types}\"\n                raise ValueError(msg)\n\n    @overload\n    def to_torch(\n        self,\n        return_type: Literal[\"tensor\"] = ...,\n        *,\n        label: str | Expr | Sequence[str | Expr] | None = ...,\n        features: str | Expr | Sequence[str | Expr] | None = ...,\n        dtype: PolarsDataType | None = ...,\n    ) -> torch.Tensor: ...\n\n    @overload\n    def to_torch(\n        self,\n        return_type: Literal[\"dataset\"],\n        *,\n        label: str | Expr | Sequence[str | Expr] | None = ...,\n        features: str | Expr | Sequence[str | Expr] | None = ...,","sourceCodeStart":2273,"sourceCodeEnd":2309,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L2273-L2309","documentation":"Raised by DataFrame.to_jax when `return_type` is not one of the allowed literals ('array', 'dict'). The parameter is typed as JaxExportType = Literal['array', 'dict'], and polars enumerates the valid values via get_args in the error message. Because Python does not enforce Literal at runtime, an invalid string from config or a typo reaches the dispatch chain and falls into the final else branch.","triggerScenarios":"`df.to_jax('tensor')` (torch vocabulary), `df.to_jax('Array')`, `df.to_jax('dataset')`, or a return_type read from a config file/CLI arg that isn't exactly 'array' or 'dict'.","commonSituations":"Copy-pasting a to_torch return_type into a to_jax call; user-configurable export functions where the string comes from YAML/JSON; case mismatches like 'Dict'; shared constants defined for one backend and reused for another.","solutions":["Use 'array' (default) or 'dict': `df.to_jax('dict')`","Validate config-driven values before the call: `assert rt in ('array', 'dict')`","If you wanted a torch tensor/dataset, use `df.to_torch(...)` instead"],"exampleFix":"# before\narr = df.to_jax('tensor')\n\n# after\narr = df.to_jax('array')\n# or, for torch:\ntensor = df.to_torch('tensor')","handlingStrategy":"validation","validationCode":"VALID_JAX = ('array', 'dict')\nif return_type not in VALID_JAX:\n    raise ValueError(f'return_type must be one of {VALID_JAX}, got {return_type!r}')\nout = df.to_jax(return_type)","typeGuard":"def is_jax_return_type(rt: object) -> bool:\n    \"\"\"to_jax only accepts 'array' or 'dict'.\"\"\"\n    return rt in ('array', 'dict')","tryCatchPattern":"try:\n    out = df.to_jax(return_type)\nexcept ValueError as e:\n    if 'invalid `return_type`' in str(e):\n        out = df.to_jax('array')\n    else:\n        raise","preventionTips":["Validate config-driven return_type strings against ('array', 'dict') before calling","Keep separate constants for jax and torch return types; never share them","Use type-annotated wrappers (Literal) so mypy catches bad literals statically"],"tags":["jax","export","invalid-argument","ml"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}