{"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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/expr/array.py#L1140-L1176","documentation":"TypeError in Expr.arr.to_struct: the fields argument must be a list of field-name strings, but a single bare string was passed. The message suggests wrapping the string in a list, e.g. ['a'] instead of 'a'.","triggerScenarios":"Calling pl.col('a').arr.to_struct('x') instead of to_struct(['x']); also occurs when a variable holding field names is sometimes a single string and sometimes a list.","commonSituations":"Defining struct field names in config as a single string; converting code from str.to_struct-like APIs that accept a single name; typo when only one field is needed.","solutions":["Wrap the string in a list: to_struct(['field_name'])","Pass None to auto-generate field names (field_0, field_1, ...)","Normalize inputs: fields if isinstance(fields, list) else [fields]","Pass a full list of names when the array width > 1"],"exampleFix":"// before\npl.col('a').arr.to_struct('coords')\n// after\npl.col('a').arr.to_struct(['coords'])","handlingStrategy":"type-guard","validationCode":"if isinstance(fields, str):\n    fields = [fields]","typeGuard":"def as_field_list(fields):\n    return list(fields) if isinstance(fields, (list, tuple)) else [fields]","tryCatchPattern":"try:\n    e = col.arr.to_struct(fields)\nexcept TypeError as e:\n    if 'got a str instead of a list' in str(e):\n        e = col.arr.to_struct([fields])\n    else:\n        raise","preventionTips":["Always pass a list to to_struct, even for a single field","Type-hint the parameter as list[str] | None","Normalize single-string config values into lists at load time"],"tags":["python","polars","type-error","arrays","struct"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}