{"record":{"id":"e96dccc1e905b695","repo":"pola-rs/polars","slug":"no-data-cannot-infer-schema","errorCode":null,"errorMessage":"no data, cannot infer schema","messagePattern":"no data, cannot infer schema","errorType":"exception","errorClass":"NoDataError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/convert/general.py","lineNumber":216,"sourceCode":"    >>> pl.from_dicts(\n    ...     data,\n    ...     schema=[\"a\", \"b\", \"c\", \"d\"],\n    ...     schema_overrides={\"c\": pl.Float64, \"d\": pl.String},\n    ... )\n    shape: (3, 4)\n    ┌─────┬─────┬──────┬──────┐\n    │ a   ┆ b   ┆ c    ┆ d    │\n    │ --- ┆ --- ┆ ---  ┆ ---  │\n    │ i64 ┆ i64 ┆ f64  ┆ str  │\n    ╞═════╪═════╪══════╪══════╡\n    │ 1   ┆ 4   ┆ null ┆ null │\n    │ 2   ┆ 5   ┆ null ┆ null │\n    │ 3   ┆ 6   ┆ null ┆ null │\n    └─────┴─────┴──────┴──────┘\n    \"\"\"\n    if not data and not (schema or schema_overrides):\n        msg = \"no data, cannot infer schema\"\n        raise NoDataError(msg)\n\n    return pl.DataFrame(\n        data,\n        schema=schema,\n        schema_overrides=schema_overrides,\n        strict=strict,\n        infer_schema_length=infer_schema_length,\n    )\n\n\ndef from_records(\n    data: Sequence[Any],\n    schema: SchemaDefinition | None = None,\n    *,\n    schema_overrides: SchemaDict | None = None,\n    strict: bool = True,\n    orient: Orientation | None = None,\n    infer_schema_length: int | None = N_INFER_DEFAULT,","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/py-polars/src/polars/convert/general.py#L198-L234","documentation":"`pl.from_dicts(data)` infers the schema from the dict contents; with an empty `data` and neither `schema` nor `schema_overrides` given, there is nothing to infer from and polars raises `NoDataError`. Supplying any schema information makes an empty input legal and yields a correctly-typed empty DataFrame.","triggerScenarios":"`pl.from_dicts([])`; `pl.from_dicts(rows)` where `rows` was filtered down to an empty list; the first batch of a looping/streaming job being empty.","commonSituations":"Dynamic pipelines where a source legitimately returns zero records; API responses that can be empty; unit tests with empty fixtures; date-range queries that match nothing.","solutions":["Pass an explicit schema: `pl.from_dicts([], schema={'id': pl.Int64, 'name': pl.String})` — this also produces a typed empty frame.","Short-circuit empty batches: `df = pl.DataFrame(schema=schema) if not data else pl.from_dicts(data, schema=schema)`.","If the data should never be empty, investigate why it is before converting."],"exampleFix":"# before\ndf = pl.from_dicts(api_results)  # api_results == [] -> NoDataError\n\n# after\nSCHEMA = {\"id\": pl.Int64, \"score\": pl.Float64}\ndf = pl.from_dicts(api_results, schema=SCHEMA)","handlingStrategy":"validation","validationCode":"SCHEMA = {\"id\": pl.Int64, \"score\": pl.Float64}\n\ndef to_frame(rows: list[dict]) -> pl.DataFrame:\n    if not rows:\n        return pl.DataFrame(schema=SCHEMA)  # typed empty frame, no inference needed\n    return pl.from_dicts(rows, schema=SCHEMA)","typeGuard":null,"tryCatchPattern":"from polars.exceptions import NoDataError\n\ntry:\n    df = pl.from_dicts(data)\nexcept NoDataError:\n    df = pl.DataFrame()  # or pl.DataFrame(schema=EXPECTED_SCHEMA)","preventionTips":["Always pass an explicit schema when converting data that can legitimately be empty.","Short-circuit empty batches in streaming loops before conversion."],"tags":["conversion","schema","empty-data","polars"],"backgroundTag":"empty-input-data","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"}