{"record":{"id":"76b9c2e767a3fd43","repo":"pola-rs/polars","slug":"list-to-struct-got-a-str-instead-of-a-list-hint","errorCode":null,"errorMessage":"list.to_struct() got a str instead of a list. hint: pass ['{fields}'] instead of '{fields}'","messagePattern":"list\\.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/list.py","lineNumber":1384,"sourceCode":"        ┌──────┬──────┐\n        │ x    ┆ y    │\n        │ ---  ┆ ---  │\n        │ i64  ┆ i64  │\n        ╞══════╪══════╡\n        │ 1    ┆ null │\n        │ 0    ┆ 1    │\n        │ 1    ┆ 0    │\n        │ null ┆ null │\n        │ null ┆ 1    │\n        │ null ┆ null │\n        └──────┴──────┘\n        \"\"\"\n        if isinstance(fields, str):\n            msg = (\n                \"list.to_struct() got a str instead of a list. \"\n                f\"hint: pass ['{fields}'] instead of '{fields}'\"\n            )\n            raise TypeError(msg)\n\n        return wrap_expr(self._pyexpr.list_to_struct(fields))\n\n    def eval(self, expr: Expr, *, parallel: bool = False) -> Expr:\n        \"\"\"\n        Run any polars expression against every lists' elements.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Parameters\n        ----------\n        expr\n            Expression to run. Note that you can select an element with `pl.element()`.\n        parallel\n            Run all expression parallel. Don't activate this blindly.\n            Parallelism is worth it if there is enough work to do per thread.\n\n            This likely should not be used in the group by context, because we already","sourceCodeStart":1366,"sourceCodeEnd":1402,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/py-polars/src/polars/expr/list.py#L1366-L1402","documentation":"Identical guard to the array variant but for Expr.list.to_struct: it rejects a plain string passed as fields, since fields must be a sequence of field names or a FieldNaming callable. The message includes a concrete hint showing how to wrap the string in a list.","triggerScenarios":"pl.col('x').list.to_struct('f') where 'f' is a str instead of ['f'] or a callable.","commonSituations":"Migrating list-of-structs code from older polars versions; shared helper functions that accept either a str or list and forward the str directly.","solutions":["Pass a list: .list.to_struct(['f'])","Use a callable naming strategy for dynamic field names","Normalize helper inputs: fields = [fields] if isinstance(fields, str) else fields"],"exampleFix":"# before\npl.col('x').list.to_struct('name')\n# after\npl.col('x').list.to_struct(['name'])","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 helper functions","Add unit tests for to_struct field naming"],"tags":["python","polars","list","to-struct","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"5d8ebabf11caea54a5c29178a64a058762f49766","analyzedAt":"2026-08-28T18:02:35.179Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}