pola-rs/polars · error

unexpected input for `strategy`: {strategy!r} Choose one of

Error message

unexpected input for `strategy`: {strategy!r}

Choose one of {'first', 'all'}

What it means

DataFrame.n_chunks(strategy=...) reports chunk counts of the frame's columns. strategy must be exactly 'first' (return the chunk count of the first column only, as an int) or 'all' (return a list with one count per column). Any other value raises ValueError listing the two allowed options. The comparison is exact and case-sensitive.

Source

Thrown at py-polars/src/polars/dataframe/frame.py:10793

        ...         "b": [0.5, 4, 10, 13],
        ...         "c": [True, True, False, True],
        ...     }
        ... )
        >>> df.n_chunks()
        1
        >>> df.n_chunks(strategy="all")
        [1, 1, 1]
        """
        if strategy == "first":
            return self._df.n_chunks()
        elif strategy == "all":
            return [s.n_chunks() for s in self.__iter__()]
        else:
            msg = (
                f"unexpected input for `strategy`: {strategy!r}"
                f"\n\nChoose one of {{'first', 'all'}}"
            )
            raise ValueError(msg)

    def max(self) -> DataFrame:
        """
        Aggregate the columns of this DataFrame to their maximum value.

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "foo": [1, 2, 3],
        ...         "bar": [6, 7, 8],
        ...         "ham": ["a", "b", "c"],
        ...     }
        ... )
        >>> df.max()
        shape: (1, 3)
        ┌─────┬─────┬─────┐
        │ foo ┆ bar ┆ ham │

View on GitHub (pinned to df599052da)

Solutions

  1. Use strategy='first' for a single int, or strategy='all' for a list of per-column counts
  2. Check spelling and case — the allowed set is exactly {'first', 'all'}
  3. Validate dynamic values against {'first', 'all'} before calling, defaulting to 'first'

Example fix

# before
chunks = df.n_chunks(strategy='columns')

# after
chunks = df.n_chunks(strategy='all')  # or strategy='first' for the first column only
Defensive patterns

Strategy: validation

Validate before calling

strategy = strategy if strategy in {'first', 'all'} else 'first'
n = df.n_chunks(strategy=strategy)

Type guard

def is_n_chunks_strategy(v: object) -> bool:
    return isinstance(v, str) and v in {'first', 'all'}

Prevention

When it happens

Trigger: df.n_chunks(strategy='columns'), strategy='per_column', strategy='min', or a variable holding a stray string; passing strategy=None expecting a default; code written against an older polars where n_chunks took no strategy argument.

Common situations: Version drift: older releases returned a per-column list by default, so old call sites guess a parameter name; forwarding display/diagnostic options from user configuration; misspelled literals in monitoring code.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/37e770b6b7ceb160. Report an issue: GitHub.