pola-rs/polars · error · TypeError

list.to_struct() got a str instead of a list. hint: pass

Error message

list.to_struct() got a str instead of a list. hint: pass ['{fields}'] instead of '{fields}'

What it means

Expr.list.to_struct() expects `fields` to be a list of field-name strings. Passing a bare string means you likely wrote the single field name directly; Polars raises this TypeError with a hint telling you to wrap the string in a list. This guards against ambiguity between one field name and several.

Solutions

  1. Wrap the string in a list: to_struct(['a']).
  2. If multiple fields, pass them in the desired order: to_struct(['a', 'b']).
  3. If you wanted default field names, omit `fields` entirely: to_struct().

Example fix

// before
pl.col('items').list.to_struct('a')
// after
pl.col('items').list.to_struct(['a'])
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(fields, str):
    fields = [fields]

Type guard

def as_field_list(fields):
    return [fields] if isinstance(fields, str) else fields

Try / catch

try:
    expr = pl.col('a').list.to_struct(fields)
except TypeError:
    expr = pl.col('a').list.to_struct([fields])

Prevention

When it happens

Trigger: Calling Expr.list.to_struct(fields='a') instead of fields=['a'].

Common situations: Single-field structs built from one-element lists; converting code that previously accepted a string; typo when defining field order for the resulting struct dtype.

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/76b9c2e767a3fd43. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/expr/list.py:1384

        ┌──────┬──────┐
        │ x    ┆ y    │
        │ ---  ┆ ---  │
        │ i64  ┆ i64  │
        ╞══════╪══════╡
        │ 1    ┆ null │
        │ 0    ┆ 1    │
        │ 1    ┆ 0    │
        │ null ┆ null │
        │ null ┆ 1    │
        │ null ┆ null │
        └──────┴──────┘
        """
        if isinstance(fields, str):
            msg = (
                "list.to_struct() got a str instead of a list. "
                f"hint: pass ['{fields}'] instead of '{fields}'"
            )
            raise TypeError(msg)

        return wrap_expr(self._pyexpr.list_to_struct(fields))

    def to_map(self) -> Expr:
        """
        Convert the `List` of `Struct` entries to a `Map`.

        The input must be a `List` of `Struct` with exactly two fields named `key`
        and `value`. Keys must not be null; duplicate keys within a row are resolved
        by keeping the first position and the last value.

        The inverse of :meth:`Expr.map.entries`.

        .. engine-support:: in-memory, streaming, distributed

        Examples
        --------
        >>> df = pl.DataFrame(

View on GitHub (pinned to fe841f959e)