{"record":{"id":"4cccb3342102e10f","repo":"pola-rs/polars","slug":"expected-data-of-type-sequence-got-type-data","errorCode":null,"errorMessage":"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)`.","messagePattern":"expected data of type Sequence, got (.+?)\n\nHint: Try passing your data to the DataFrame constructor instead, e\\.g\\. `pl\\.DataFrame\\(data\\)`\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/convert/general.py","lineNumber":297,"sourceCode":"    >>> df\n    shape: (3, 2)\n    ┌─────┬─────┐\n    │ a   ┆ b   │\n    │ --- ┆ --- │\n    │ i64 ┆ i64 │\n    ╞═════╪═════╡\n    │ 1   ┆ 4   │\n    │ 2   ┆ 5   │\n    │ 3   ┆ 6   │\n    └─────┴─────┘\n    \"\"\"\n    if not isinstance(data, Sequence):\n        msg = (\n            f\"expected data of type Sequence, got {type(data).__name__!r}\"\n            \"\\n\\nHint: Try passing your data to the DataFrame constructor instead,\"\n            \" e.g. `pl.DataFrame(data)`.\"\n        )\n        raise TypeError(msg)\n\n    return wrap_df(\n        sequence_to_pydf(\n            data,\n            schema=schema,\n            schema_overrides=schema_overrides,\n            strict=strict,\n            orient=orient,\n            infer_schema_length=infer_schema_length,\n        )\n    )\n\n\ndef from_numpy(\n    data: np.ndarray[Any, Any],\n    schema: SchemaDefinition | None = None,\n    *,\n    schema_overrides: SchemaDict | None = None,","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/py-polars/src/polars/convert/general.py#L279-L315","documentation":"`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.","triggerScenarios":"`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).","commonSituations":"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`).","solutions":["Use the constructor, which handles generators, dicts, and numpy: `pl.DataFrame(data, orient='row')`.","Materialize lazy inputs: `pl.from_records(list(generator))`.","For dict-of-lists (columns orientation), use `pl.from_dict(data)` or `pl.DataFrame(data)`."],"exampleFix":"# before\ndf = pl.from_records((extract(r) for r in raw))  # generator -> TypeError\n\n# after\ndf = pl.DataFrame(\n    [extract(r) for r in raw], schema={\"a\": pl.Int64, \"b\": pl.String}, orient=\"row\"\n)","handlingStrategy":"type-guard","validationCode":"from collections.abc import Sequence\n\ndef records_to_df(data) -> pl.DataFrame:\n    if not isinstance(data, Sequence):\n        data = list(data)  # materialize generators/sets\n    return pl.from_records(data)","typeGuard":"from collections.abc import Sequence\nfrom typing import Any, TypeGuard\n\ndef is_record_sequence(data: Any) -> TypeGuard[Sequence[Any]]:\n    return isinstance(data, Sequence)","tryCatchPattern":null,"preventionTips":["Prefer the pl.DataFrame constructor for heterogeneous inputs — it accepts generators, dicts, and numpy arrays.","Materialize generators with list() before from_records.","Use pl.from_dict for the dict-of-columns orientation."],"tags":["conversion","typeerror","input-type","numpy","polars"],"backgroundTag":"invalid-input-type","analyzedSha":"5d8ebabf11caea54a5c29178a64a058762f49766","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}