{"record":{"id":"67af37abcd0b4aa2","repo":"pola-rs/polars","slug":"format-must-be-one-of-binary-json-got","errorCode":null,"errorMessage":"`format` must be one of {{'binary', 'json'}}, got {format!r}","messagePattern":"`format` must be one of (.+?)\\}, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/meta.py","lineNumber":363,"sourceCode":"\n        >>> expr = pl.col(\"foo\").sum().over(\"bar\")\n        >>> bytes = expr.meta.serialize()\n        >>> type(bytes)\n        <class 'bytes'>\n\n        The bytes can later be deserialized back into an `Expr` object.\n\n        >>> import io\n        >>> pl.Expr.deserialize(io.BytesIO(bytes))\n        <Expr ['col(\"foo\").sum().over([col(\"ba…'] at ...>\n        \"\"\"\n        if format == \"binary\":\n            serializer = self._pyexpr.serialize_binary\n        elif format == \"json\":\n            serializer = self._pyexpr.serialize_json\n        else:\n            msg = f\"`format` must be one of {{'binary', 'json'}}, got {format!r}\"\n            raise ValueError(msg)\n\n        return serialize_polars_object(serializer, file, format)\n\n    @overload\n    def write_json(self, file: None = ...) -> str: ...\n\n    @overload\n    def write_json(self, file: IOBase | str | Path) -> None: ...\n\n    @deprecated(\"`meta.write_json` was renamed; use `meta.serialize` instead\")\n    def write_json(self, file: IOBase | str | Path | None = None) -> str | None:\n        \"\"\"\n        Write expression to json.\n\n        .. deprecated:: 0.20.11\n            This method has been renamed to :meth:`serialize`.\n        \"\"\"\n        return self.serialize(file, format=\"json\")","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/meta.py#L345-L381","documentation":"Expr.meta.serialize accepts exactly two formats — 'binary' (default, MessagePack-based) and 'json' — and dispatches to the matching Rust serializer before writing via serialize_polars_object. Any other format string (typos like 'jsonl', 'pickle', 'yaml') hits the else branch and raises ValueError. The output is an expression tree dump used to persist or inspect logical plans.","triggerScenarios":"expr.meta.serialize(file, format='msgpack'), format='text', or a format variable that defaulted to None.","commonSituations":"Guessing format names from other polars APIs (scan_parquet-style strings); config-driven serialization where the format key is optional and resolves to None; version drift: write_json was renamed to meta.serialize, so call sites were hand-migrated and the format value mistyped.","solutions":["Use format='binary' (compact) or format='json' (readable) — or omit format for binary.","If you need JSON text into a string, meta.serialize() with format='json' and no file returns it.","Validate the config value against {'binary', 'json'} before it reaches polars."],"exampleFix":"# before\nexpr.meta.serialize(buf, format='msgpack')\n\n# after\nexpr.meta.serialize(buf, format='binary')\n# or\njson_str = expr.meta.serialize(format='json')","handlingStrategy":"validation","validationCode":"assert format in ('binary', 'json'), f'bad serialize format: {format!r}'","typeGuard":"def is_serialize_format(f: str) -> bool:\n    return f in ('binary', 'json')","tryCatchPattern":null,"preventionTips":["Only 'binary' and 'json' exist; there is no 'msgpack' or 'text'.","Note meta.write_json is deprecated — meta.serialize(format='json') is the replacement."],"tags":["validation","polars","expression","serialization","meta"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}