pola-rs/polars · error · ValueError

Cannot set `iter_batches` for

Error message

Cannot set `iter_batches` for {self.driver_name} without also setting a non-zero `batch_size`

What it means

Raised by `_from_arrow` in polars' database executor when `iter_batches=True` is requested for a driver whose Arrow interface either doesn't expose exact batch sizes or wasn't given a non-zero `batch_size`. Streaming in batches is only meaningful — and for some drivers only possible — when polars can request a concrete batch size from the driver.

Solutions

  1. Pass a positive `batch_size`, e.g. `pl.read_database(query, conn, iter_batches=True, batch_size=10_000)`.
  2. If you don't need streaming, drop `iter_batches=True` and fetch the whole result as one DataFrame.
  3. Check the driver's capabilities: if the driver doesn't support exact batch sizes, avoid `iter_batches` for it.

Example fix

// before
reader = pl.read_database(query, conn, iter_batches=True)
// after
reader = pl.read_database(query, conn, iter_batches=True, batch_size=10_000)
Defensive patterns

Strategy: validation

Validate before calling

def validate_batch_args(args: dict) -> dict:
    if args.get("iter_batches") and not args.get("batch_size"):
        raise ValueError("iter_batches=True requires a positive batch_size")
    return args

validate_batch_args({"iter_batches": True, "batch_size": 10_000})

Type guard

def iter_batches_usable(batch_size: int | None) -> bool:
    return batch_size is not None and batch_size > 0

Try / catch

try:
    df = pl.read_database(query, conn, iter_batches=True, batch_size=batch_size)
except ValueError as e:
    if "batch_size" in str(e):
        df = pl.read_database(query, conn)  # fallback: single fetch
    else:
        raise

Prevention

When it happens

Trigger: Calling `pl.read_database(query, connection, iter_batches=True)` with an Arrow-capable driver where `driver_properties["exact_batch_size"]` is true (or iter_batches was set) while `batch_size` is None or 0. E.g. `pl.read_database(q, conn, iter_batches=True)` without `batch_size=`.

Common situations: Users streaming large result sets with adbc/connectorx drivers, copying the generic iter_batches recipe from docs but forgetting `batch_size`, or passing `batch_size=0` thinking it means 'default'.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/711c8a519f44edf0. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/database/_executor.py:251

                        "adbc_driver_manager" if driver == "adbc" else self.driver_name
                    )
                    # if the minimum version constraint is not met, try additional
                    # driver properties with lower constraints
                    try:
                        self._check_module_version(driver_to_check, ver)
                    except ModuleUpgradeRequiredError:
                        if i < len(driver_properties_list):
                            continue
                        raise

                if iter_batches and (
                    driver_properties["exact_batch_size"] and not batch_size
                ):
                    msg = (
                        f"Cannot set `iter_batches` for {self.driver_name} "
                        "without also setting a non-zero `batch_size`"
                    )
                    raise ValueError(msg)  # noqa: TRY301

                frames = (
                    self._apply_overrides(batch, (schema_overrides or {}))
                    if isinstance(batch, DataFrame)
                    else self._apply_overrides(
                        DataFrame(batch), (schema_overrides or {})
                    )
                    for batch in self._fetch_arrow(
                        driver_properties,
                        iter_batches=iter_batches,
                        batch_size=batch_size,
                    )
                )
                return frames if iter_batches else next(frames)  # type: ignore[arg-type,return-value]
        except Exception as err:
            # eg: valid turbodbc/snowflake connection, but no arrow support
            # compiled in to the underlying driver (or on this connection)
            arrow_not_supported = (

View on GitHub (pinned to fe841f959e)