pola-rs/polars · error · TypeError
arr.to_struct() got a str instead of a list. hint: pass
Error message
arr.to_struct() got a str instead of a list. hint: pass ['{fields}'] instead of '{fields}' What it means
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'.
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
Example fix
// before
pl.col('a').arr.to_struct('coords')
// after
pl.col('a').arr.to_struct(['coords']) Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(fields, str):
fields = [fields] Type guard
def as_field_list(fields):
return list(fields) if isinstance(fields, (list, tuple)) else [fields] Try / catch
try:
e = col.arr.to_struct(fields)
except TypeError as e:
if 'got a str instead of a list' in str(e):
e = col.arr.to_struct([fields])
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- expected type 'int | str', got
- arr.dot query vector must be one-dimensional
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
- cannot call `map_groups` when grouping by named expressions
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/e5f89c3c56cb0b78.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/expr/array.py:1158
shape: (5, 3)
┌──────┬──────┬──────┐
│ x ┆ y ┆ z │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞══════╪══════╪══════╡
│ 1 ┆ 0 ┆ 0 │
│ 0 ┆ 1 ┆ 0 │
│ 0 ┆ 0 ┆ 1 │
│ null ┆ null ┆ null │
│ null ┆ null ┆ null │
└──────┴──────┴──────┘
"""
if isinstance(fields, str):
msg = (
"arr.to_struct() got a str instead of a list. "
f"hint: pass ['{fields}'] instead of '{fields}'"
)
raise TypeError(msg)
pyexpr = self._pyexpr.arr_to_struct(fields)
return wrap_expr(pyexpr)
def shift(self, n: int | IntoExprColumn = 1) -> Expr:
"""
Shift array values by the given number of indices.
.. engine-support:: in-memory, streaming, distributed
Parameters
----------
n
Number of indices to shift forward. If a negative value is passed, values
are shifted in the opposite direction instead.
Notes
-----View on GitHub (pinned to fe841f959e)