{"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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/convert/general.py#L198-L234","documentation":"pl.from_dicts raises NoDataError when the input list is empty AND neither a schema nor schema_overrides is provided, because column names and types cannot be inferred from zero rows. Supplying a schema removes the ambiguity and makes empty input valid.","triggerScenarios":"pl.from_dicts([]) with no schema argument; also falsy inputs like None or empty sequences with schema=None and schema_overrides=None.","commonSituations":"Building a DataFrame from API/DB results that returned no rows; aggregating per-partition dicts where every partition happened to be empty.","solutions":["Provide a schema: pl.from_dicts([], schema={'a': pl.Int64, 'b': pl.Utf8}).","Provide schema_overrides for the expected columns.","Guard: return pl.DataFrame(schema=...) or None when the input list is empty.","Catch polars.NoDataError and substitute an empty typed DataFrame."],"exampleFix":"// before\n df = pl.from_dicts(records)  # records == []\n// after\n df = pl.from_dicts(records, schema={\"id\": pl.Int64, \"name\": pl.String})","handlingStrategy":"try-catch","validationCode":"if not data and not (schema or schema_overrides):\n    # supply an empty typed frame instead\n    df = pl.DataFrame(schema=schema or {\"a\": pl.String})\nelse:\n    df = pl.from_dicts(data, schema=schema, schema_overrides=schema_overrides)","typeGuard":"def has_schema_kwargs(schema, schema_overrides) -> bool:\n    return schema is not None or schema_overrides is not None","tryCatchPattern":"from polars.exceptions import NoDataError\ntry:\n    df = pl.from_dicts(records)\nexcept NoDataError:\n    df = pl.DataFrame(schema=expected_schema)","preventionTips":["Always pass an explicit schema when data may be empty.","Guard empty result sets from APIs/DB queries before constructing frames.","Catch polars.exceptions.NoDataError at boundaries where emptiness is expected."],"tags":["python","polars","empty-input","schema-inference"],"backgroundTag":"empty-required-field","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}