pola-rs/polars · error · ValueError

`format` must be one of

Error message

`format` must be one of {'binary', 'json'}, got {format!r}

What it means

Expr.meta.serialize() only accepts format='binary' or format='json'. Any other value falls into the else branch and raises this ValueError. Serialization backends are enumerated, so unknown formats are rejected before touching the file/stream.

Solutions

  1. Use format='binary' (default) or format='json'.
  2. Fix casing: pass 'json' not 'JSON'.
  3. Validate the format against a literal set before calling serialize.

Example fix

// before
expr.meta.serialize(f, format='yaml')
// after
expr.meta.serialize(f, format='json')
Defensive patterns

Strategy: validation

Validate before calling

VALID_FORMATS = {'binary', 'json'}
if format not in VALID_FORMATS:
    raise ValueError(f'format must be one of {VALID_FORMATS}, got {format!r}')

Type guard

from typing import Literal
def is_valid_format(f: str) -> bool:
    return f in ('binary', 'json')

Try / catch

try:
    expr.meta.serialize(file, format=fmt)
except ValueError:
    expr.meta.serialize(file, format='binary')

Prevention

When it happens

Trigger: Calling Expr.meta.serialize(file, format='yaml') (or 'txt', 'pickle', '', None, case variants like 'JSON').

Common situations: Assuming YAML/other formats are supported; passing a format variable read from config with wrong casing; typos like 'bin' or 'js'.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/expr/meta.py:449

        >>> expr = pl.col("foo").sum().over("bar")
        >>> bytes = expr.meta.serialize()
        >>> type(bytes)
        <class 'bytes'>

        The bytes can later be deserialized back into an `Expr` object.

        >>> import io
        >>> pl.Expr.deserialize(io.BytesIO(bytes))
        <Expr ['col("foo").sum().over([col("ba…'] at ...>
        """
        if format == "binary":
            serializer = self._pyexpr.serialize_binary
        elif format == "json":
            serializer = self._pyexpr.serialize_json
        else:
            msg = f"`format` must be one of {{'binary', 'json'}}, got {format!r}"
            raise ValueError(msg)

        return serialize_polars_object(serializer, file, format)

    @overload
    def tree_format(
        self,
        *,
        return_as_string: Literal[False] = ...,
        schema: None | SchemaDict = None,
    ) -> None: ...

    @overload
    def tree_format(
        self, *, return_as_string: Literal[True], schema: None | SchemaDict = None
    ) -> str: ...

    def tree_format(
        self, *, return_as_string: bool = False, schema: None | SchemaDict = None

View on GitHub (pinned to fe841f959e)