{"record":{"id":"e5f89c3c56cb0b78","repo":"pola-rs/polars","slug":"arr-to-struct-got-a-str-instead-of-a-list-hint","errorCode":null,"errorMessage":"arr.to_struct() got a str instead of a list. hint: pass ['{fields}'] instead of '{fields}'","messagePattern":"arr\\.to_struct\\(\\) got a str instead of a list\\. hint: pass \\['(.+?)'\\] instead of '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/array.py","lineNumber":1158,"sourceCode":"        shape: (5, 3)\n        ┌──────┬──────┬──────┐\n        │ x    ┆ y    ┆ z    │\n        │ ---  ┆ ---  ┆ ---  │\n        │ i64  ┆ i64  ┆ i64  │\n        ╞══════╪══════╪══════╡\n        │ 1    ┆ 0    ┆ 0    │\n        │ 0    ┆ 1    ┆ 0    │\n        │ 0    ┆ 0    ┆ 1    │\n        │ null ┆ null ┆ null │\n        │ null ┆ null ┆ null │\n        └──────┴──────┴──────┘\n        \"\"\"\n        if isinstance(fields, str):\n            msg = (\n                \"arr.to_struct() got a str instead of a list. \"\n                f\"hint: pass ['{fields}'] instead of '{fields}'\"\n            )\n            raise TypeError(msg)\n\n        pyexpr = self._pyexpr.arr_to_struct(fields)\n        return wrap_expr(pyexpr)\n\n    def shift(self, n: int | IntoExprColumn = 1) -> Expr:\n        \"\"\"\n        Shift array values by the given number of indices.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Parameters\n        ----------\n        n\n            Number of indices to shift forward. If a negative value is passed, values\n            are shifted in the opposite direction instead.\n\n        Notes\n        -----","sourceCodeStart":1140,"sourceCodeEnd":1176,"githubUrl":"https://github.com/pola-rs/polars/blob/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/expr/array.py#L1140-L1176","documentation":"TypeError raised by Expr.arr.to_struct when the fields argument is a plain string. Historically a string field-name template (e.g. 'field_n') was accepted; now fields must be a sequence of names or a FieldNaming callable, and polars explicitly hints to wrap the string in a list.","triggerScenarios":"df.select(pl.col('a').arr.to_struct('field_')) — passing a str where a list of field names or callable is expected.","commonSituations":"Code written against older polars where a str template was valid; copy-paste from outdated Stack Overflow answers; intending a single custom name for one-element structs.","solutions":["Wrap in a list: .arr.to_struct(['field_'])","Or pass a naming callable like .arr.to_struct(lambda idx: f'f{idx}')","Or omit fields entirely to use the default field_N naming"],"exampleFix":"# before\npl.col('lists').arr.to_struct('field_')\n# after\npl.col('lists').arr.to_struct(['field_'])","handlingStrategy":"type-guard","validationCode":"fields = [fields] if isinstance(fields, str) else fields","typeGuard":"def valid_fields(f) -> bool:\n    import collections.abc as abc\n    return callable(f) or (isinstance(f, abc.Sequence) and not isinstance(f, str))","tryCatchPattern":null,"preventionTips":["Always pass a list of names or a callable","Normalize str->list in shared helpers","Check the current signature after polars upgrades"],"tags":["python","polars","array","to-struct","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-08-28T18:02:35.179Z","contentChangedAt":"2026-08-28T18:02:35.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}