{"record":{"id":"93979b60326f7721","repo":"pola-rs/polars","slug":"cannot-set-iter-batches-without-also-setting-a-n","errorCode":null,"errorMessage":"Cannot set `iter_batches` without also setting a non-zero `batch_size`","messagePattern":"Cannot set `iter_batches` without also setting a non-zero `batch_size`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/database/_executor.py","lineNumber":293,"sourceCode":"\n        return None\n\n    def _from_rows(\n        self,\n        *,\n        batch_size: int | None,\n        iter_batches: bool,\n        schema_overrides: SchemaDict | None,\n        infer_schema_length: int | None,\n    ) -> DataFrame | Iterator[DataFrame] | None:\n        \"\"\"Return resultset data row-wise for frame init.\"\"\"\n        from polars import DataFrame\n\n        if iter_batches and not batch_size:\n            msg = (\n                \"Cannot set `iter_batches` without also setting a non-zero `batch_size`\"\n            )\n            raise ValueError(msg)\n\n        if is_async := isinstance(original_result := self.result, Coroutine):\n            self.result = _run_async(self.result)\n        try:\n            if hasattr(self.result, \"fetchall\"):\n                if is_alchemy := (self.driver_name == \"sqlalchemy\"):\n                    if hasattr(self.result, \"cursor\"):\n                        cursor_desc = [\n                            (d[0], d[1:]) for d in self.result.cursor.description\n                        ]\n                    elif hasattr(self.result, \"_metadata\"):\n                        cursor_desc = [(k, None) for k in self.result._metadata.keys]\n                    else:\n                        msg = f\"Unable to determine metadata from query result; {self.result!r}\"\n                        raise ValueError(msg)\n\n                elif hasattr(self.result, \"description\"):\n                    cursor_desc = [(d[0], d[1:]) for d in self.result.description]","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/database/_executor.py#L275-L311","documentation":"Raised by `_from_rows` (the non-Arrow fetch path of the database executor) when `iter_batches=True` is requested but `batch_size` is None or zero. Unlike the Arrow path, no driver name is cited — this is a plain precondition check: batched iteration is impossible without a batch size to chunk rows into.","triggerScenarios":"Calling `pl.read_database(query, connection, iter_batches=True)` (or the executor's `_from_rows` directly) on a driver that goes through the row-based fetch path (e.g. plain DB-API cursors, sqlalchemy without Arrow support) while omitting `batch_size` or passing `batch_size=0`.","commonSituations":"Chunked reading of large MySQL/Postgres/SQLite results with a DB-API cursor, forgetting the `batch_size` kwarg; copy-pasting `iter_batches=True` from an Arrow example; passing 0 as 'unlimited' by mistake.","solutions":["Provide a non-zero `batch_size`, e.g. `pl.read_database(query, conn, iter_batches=True, batch_size=50_000)`.","Remove `iter_batches=True` if a single DataFrame is acceptable.","If memory limits motivated batching, size `batch_size` from estimated row width × available memory."],"exampleFix":"// before\nfor df in pl.read_database(query, conn, iter_batches=True):\n    process(df)\n// after\nfor df in pl.read_database(query, conn, iter_batches=True, batch_size=50_000):\n    process(df)","handlingStrategy":"validation","validationCode":"def require_batch_size(iter_batches: bool, batch_size: int | None) -> None:\n    if iter_batches and not batch_size:\n        raise ValueError(\"iter_batches requires a non-zero batch_size\")\n\nrequire_batch_size(iter_batches=True, batch_size=50_000)","typeGuard":"def has_valid_batch_size(batch_size: int | None) -> bool:\n    return isinstance(batch_size, int) and batch_size > 0","tryCatchPattern":"try:\n    frames = pl.read_database(query, conn, iter_batches=True, batch_size=n)\nexcept ValueError as e:\n    if \"batch_size\" in str(e):\n        frames = [pl.read_database(query, conn)]\n    else:\n        raise","preventionTips":["Always set batch_size when enabling iter_batches on row-based (non-Arrow) drivers.","Centralize read_database calls in one helper that validates the kwarg combination.","Set batch_size relative to row width and memory budget, not arbitrarily small values."],"tags":["polars","database","batching","invalid-arguments","python"],"backgroundTag":"conflicting-config-options","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"}