pola-rs/polars · error

number of rows per chunk must be >= 1

Error message

number of rows per chunk must be >= 1

What it means

Config.set_streaming_chunk_size validates the morsel size used by the streaming engine before writing the POLARS_IDEAL_MORSEL_SIZE environment variable. Passing a size smaller than 1 is meaningless (a chunk cannot hold zero or negative rows), so a ValueError is raised. Setting it to None removes the env var and is allowed.

Solutions

  1. Pass an integer >= 1, e.g. pl.Config.set_streaming_chunk_size(100_000).
  2. Pass None to reset to the default instead of 0.
  3. Guard dynamic computations: max(1, total_rows // workers).

Example fix

// before
pl.Config.set_streaming_chunk_size(len(df) // 32)  # 0 when df is small
// after
pl.Config.set_streaming_chunk_size(max(1, len(df) // 32))
Defensive patterns

Strategy: validation

Validate before calling

if chunk_size is not None and chunk_size < 1:
    raise ValueError("chunk_size must be >= 1")
pl.Config.set_streaming_chunk_size(chunk_size)

Type guard

def is_valid_chunk_size(v) -> bool:
    return v is None or (isinstance(v, int) and v >= 1)

Try / catch

try:
    pl.Config.set_streaming_chunk_size(n)
except ValueError:
    pl.Config.set_streaming_chunk_size(None)  # fall back to default

Prevention

When it happens

Trigger: Calling pl.Config.set_streaming_chunk_size(0), a negative integer, or any value < 1.

Common situations: Computing the chunk size dynamically (e.g. rows // num_workers) where the divisor exceeds the row count, yielding 0; copy-pasting tuning snippets with a placeholder value.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/config.py:960

        Overwrite chunk size used in `streaming` engine.

        By default, the chunk size is determined by the schema
        and size of the thread pool. For some datasets (esp.
        when you have large string elements) this can be too
        optimistic and lead to Out of Memory errors.

        Parameters
        ----------
        size
            Number of rows per chunk. Every thread will process chunks
            of this size.
        """
        if size is None:
            os.environ.pop("POLARS_IDEAL_MORSEL_SIZE", None)
        else:
            if size < 1:
                msg = "number of rows per chunk must be >= 1"
                raise ValueError(msg)

            os.environ["POLARS_IDEAL_MORSEL_SIZE"] = str(size)
        plr.config_reload_env_var("POLARS_IDEAL_MORSEL_SIZE")
        return cls

    @classmethod
    def set_tbl_cell_alignment(cls, format: Alignment | None) -> type[Config]:
        """
        Set table cell alignment.

        Parameters
        ----------
        format : str
            * "LEFT": left aligned
            * "CENTER": center aligned
            * "RIGHT": right aligned

        Examples

View on GitHub (pinned to fe841f959e)