{"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":449,"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 tree_format(\n        self,\n        *,\n        return_as_string: Literal[False] = ...,\n        schema: None | SchemaDict = None,\n    ) -> None: ...\n\n    @overload\n    def tree_format(\n        self, *, return_as_string: Literal[True], schema: None | SchemaDict = None\n    ) -> str: ...\n\n    def tree_format(\n        self, *, return_as_string: bool = False, schema: None | SchemaDict = None","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/pola-rs/polars/blob/fc24390824009e8fc5b7870e256273c1b140a019/py-polars/src/polars/expr/meta.py#L431-L467","documentation":"ValueError raised by Expr.meta.serialize when the format argument is neither 'binary' nor 'json'. Only those two serialization formats are implemented for expression trees, and anything else (including common mistakes like 'yaml', 'jsonl', or 'text') is rejected with the received value echoed back.","triggerScenarios":"pl.col('a').sum().meta.serialize(format='yaml'), or passing file paths/None into the format parameter, or a variable that silently evaluated to an unexpected string.","commonSituations":"Generic serialize/deserialize wrapper functions defaulting format to something else; confusion with DataFrame.write_json or serde formats used elsewhere in polars.","solutions":["Use format='binary' (compact) or format='json' (readable)","Default the parameter in wrappers to one of the two valid values","Validate with a whitelist before calling serialize in generic pipelines"],"exampleFix":"# before\nexpr.meta.serialize(format='yaml')\n# after\nexpr.meta.serialize(format='json')","handlingStrategy":"validation","validationCode":"assert format in ('binary', 'json'), f'bad format {format!r}'","typeGuard":"def is_valid_format(fmt: object) -> bool:\n    return fmt in ('binary', 'json')","tryCatchPattern":"try:\n    expr.meta.serialize(format=fmt)\nexcept ValueError:\n    fmt = 'json'\n    expr.meta.serialize(format=fmt)","preventionTips":["Default wrapper parameters to 'binary' or 'json'","Whitelist before serializing dynamically chosen formats"],"tags":["python","polars","serialize","expression","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"fc24390824009e8fc5b7870e256273c1b140a019","analyzedAt":"2026-08-28T18:02:35.179Z","contentChangedAt":"2026-08-28T18:02:35.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}