{"record":{"id":"435a03029cf59bba","repo":"pola-rs/polars","slug":"label-is-required-if-setting-features-when-re","errorCode":null,"errorMessage":"`label` is required if setting `features` when `return_type='dict'","messagePattern":"`label` is required if setting `features` when `return_type='dict'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":2238,"sourceCode":"        ...     return_type=\"dict\",\n        ...     features=cs.float(),\n        ...     label=pl.col(\"lbl\").cast(pl.UInt8),\n        ... )\n        {'label': Array([[0],\n                [1],\n                [2],\n                [3]], dtype=uint8),\n         'features': Array([[ 1.5 ],\n                [-0.5 ],\n                [ 0.  ],\n                [-2.25]], dtype=float32)}\n        \"\"\"\n        if return_type != \"dict\" and (label is not None or features is not None):\n            msg = \"`label` and `features` only apply when `return_type` is 'dict'\"\n            raise ValueError(msg)\n        elif return_type == \"dict\" and label is None and features is not None:\n            msg = \"`label` is required if setting `features` when `return_type='dict'\"\n            raise ValueError(msg)\n\n        jx = import_optional(\n            \"jax\",\n            install_message=\"Please see `https://jax.readthedocs.io/en/latest/installation.html` \"\n            \"for specific installation recommendations for the Jax package\",\n        )\n        enabled_double_precision = jx.config.jax_enable_x64 or bool(\n            int(os.environ.get(\"JAX_ENABLE_X64\", \"0\"))\n        )\n        if dtype:\n            frame = self.cast(dtype)\n        elif not enabled_double_precision:\n            # enforce single-precision unless environment/config directs otherwise\n            frame = self.cast({Float64: Float32, Int64: Int32, UInt64: UInt32})\n        else:\n            frame = self\n\n        if isinstance(device, str):","sourceCodeStart":2220,"sourceCodeEnd":2256,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L2220-L2256","documentation":"Raised by DataFrame.to_jax(return_type='dict') when `features` is given but `label` is None. The dict export builds its split around the label column (features default to 'everything except label'), so features without a label has no well-defined split. Polars requires the symmetric pairing: either both label and features, label alone, or neither.","triggerScenarios":"`df.to_jax('dict', features=['a','b'])` with no label; passing label=None explicitly with a features list; building kwargs dynamically where the label entry is omitted on some code path.","commonSituations":"Unsupervised-learning code paths reusing a supervised export helper and only setting features; feature lists computed from column names where the label variable is accidentally None; config-driven pipelines where 'label' is optional but 'features' is always set.","solutions":["Provide a label: `df.to_jax('dict', label='y', features=['a','b'])`","If there is no label, use plain column selection: `df.select(['a','b']).to_jax()` or `df.to_jax('dict', label='y')` only","Guard kwargs construction: only include features when label is also present"],"exampleFix":"# before\nout = df.to_jax('dict', features=['f1', 'f2'])\n\n# after\nout = df.select(['f1', 'f2']).to_jax()   # no label concept needed","handlingStrategy":"validation","validationCode":"if return_type == 'dict' and features is not None and label is None:\n    raise ValueError('to_jax dict export needs a label when features are given')\nout = df.to_jax(return_type, label=label, features=features)","typeGuard":null,"tryCatchPattern":"try:\n    out = df.to_jax('dict', label=label, features=features)\nexcept ValueError as e:\n    if '`label` is required' in str(e):\n        out = df.select(features or df.columns).to_jax()\n    else:\n        raise","preventionTips":["Treat label and features as a pair for dict exports: set both or just label","For label-free exports, select columns and call to_jax() without split args","Centralize model-export config so the label/features pairing is validated once"],"tags":["jax","export","argument-mismatch","ml"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}