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

  1. Wrap the string in a list: to_struct(['field_name'])
  2. Pass None to auto-generate field names (field_0, field_1, ...)
  3. Normalize inputs: fields if isinstance(fields, list) else [fields]
  4. 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

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


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)