pola-rs/polars · error · TypeError

expected data of type Sequence, got {type(data).__name__!r}

Error message

expected data of type Sequence, got {type(data).__name__!r}

Hint: Try passing your data to the DataFrame constructor instead, e.g. `pl.DataFrame(data)`.

What it means

`pl.from_records(data)` requires a `collections.abc.Sequence` (list, tuple, etc.) because it indexes and len()s the input; generators, sets, dicts, and even numpy arrays are not Sequences. Non-Sequences raise TypeError with a hint to use the `pl.DataFrame` constructor, which accepts all of those shapes.

Source

Thrown at py-polars/src/polars/convert/general.py:297

    >>> df
    shape: (3, 2)
    ┌─────┬─────┐
    │ a   ┆ b   │
    │ --- ┆ --- │
    │ i64 ┆ i64 │
    ╞═════╪═════╡
    │ 1   ┆ 4   │
    │ 2   ┆ 5   │
    │ 3   ┆ 6   │
    └─────┴─────┘
    """
    if not isinstance(data, Sequence):
        msg = (
            f"expected data of type Sequence, got {type(data).__name__!r}"
            "\n\nHint: Try passing your data to the DataFrame constructor instead,"
            " e.g. `pl.DataFrame(data)`."
        )
        raise TypeError(msg)

    return wrap_df(
        sequence_to_pydf(
            data,
            schema=schema,
            schema_overrides=schema_overrides,
            strict=strict,
            orient=orient,
            infer_schema_length=infer_schema_length,
        )
    )


def from_numpy(
    data: np.ndarray[Any, Any],
    schema: SchemaDefinition | None = None,
    *,
    schema_overrides: SchemaDict | None = None,

View on GitHub (pinned to 5d8ebabf11)

Solutions

  1. Use the constructor, which handles generators, dicts, and numpy: `pl.DataFrame(data, orient='row')`.
  2. Materialize lazy inputs: `pl.from_records(list(generator))`.
  3. For dict-of-lists (columns orientation), use `pl.from_dict(data)` or `pl.DataFrame(data)`.

Example fix

# before
df = pl.from_records((extract(r) for r in raw))  # generator -> TypeError

# after
df = pl.DataFrame(
    [extract(r) for r in raw], schema={"a": pl.Int64, "b": pl.String}, orient="row"
)
Defensive patterns

Strategy: type-guard

Validate before calling

from collections.abc import Sequence

def records_to_df(data) -> pl.DataFrame:
    if not isinstance(data, Sequence):
        data = list(data)  # materialize generators/sets
    return pl.from_records(data)

Type guard

from collections.abc import Sequence
from typing import Any, TypeGuard

def is_record_sequence(data: Any) -> TypeGuard[Sequence[Any]]:
    return isinstance(data, Sequence)

Prevention

When it happens

Trigger: `pl.from_records(row for row in rows)` (generator); `pl.from_records(np.array([[1, 2], [3, 4]]))` (ndarray is not a Sequence); `pl.from_records({'a': [1, 2]})` (dict-of-lists).

Common situations: Feeding generators to save memory in ETL jobs; passing 2-D numpy arrays; assuming the dict-of-columns orientation works here (it belongs to `pl.from_dict`/`pl.DataFrame`).

Related errors


AI-assisted analysis of pola-rs/polars@5d8ebabf11 (2026-08-19). Data as JSON: /api/errors/4cccb3342102e10f. Report an issue: GitHub.