{"record":{"id":"77c8da079b322228","repo":"pola-rs/polars","slug":"encoding-must-be-one-of-hex-base64-got","errorCode":null,"errorMessage":"`encoding` must be one of {'hex', 'base64'}, got {encoding!r}","messagePattern":"`encoding` must be one of (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/binary.py","lineNumber":225,"sourceCode":"        ... )\n        shape: (3, 3)\n        ┌────────┬───────────┬─────────────────┐\n        │ name   ┆ encoded   ┆ code            │\n        │ ---    ┆ ---       ┆ ---             │\n        │ str    ┆ binary    ┆ binary          │\n        ╞════════╪═══════════╪═════════════════╡\n        │ black  ┆ b\"000000\" ┆ b\"\\x00\\x00\\x00\" │\n        │ yellow ┆ b\"ffff00\" ┆ b\"\\xff\\xff\\x00\" │\n        │ blue   ┆ b\"0000ff\" ┆ b\"\\x00\\x00\\xff\" │\n        └────────┴───────────┴─────────────────┘\n        \"\"\"\n        if encoding == \"hex\":\n            return wrap_expr(self._pyexpr.bin_hex_decode(strict))\n        elif encoding == \"base64\":\n            return wrap_expr(self._pyexpr.bin_base64_decode(strict))\n        else:\n            msg = f\"`encoding` must be one of {{'hex', 'base64'}}, got {encoding!r}\"\n            raise ValueError(msg)\n\n    def encode(self, encoding: TransferEncoding) -> Expr:\n        r\"\"\"\n        Encode a value using the provided encoding.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Parameters\n        ----------\n        encoding : {'hex', 'base64'}\n            The encoding to use.\n\n        Returns\n        -------\n        Expr\n            Expression of data type :class:`Binary`.\n\n        Examples","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/binary.py#L207-L243","documentation":"Expr.bin.decode supports exactly two transfer encodings, 'hex' and 'base64'; any other encoding string raises ValueError before dispatching to Rust. The comparison is exact and case-sensitive, and this namespace is for binary transfer encodings — not text/character decoding.","triggerScenarios":"pl.col('b').bin.decode('utf8'), .bin.decode('base32'), or .bin.decode('HEX') — anything not exactly 'hex' or 'base64'.","commonSituations":"Confusing transfer encoding with character encoding (wanting pandas-style str.decode for text); typos; uppercase variants; expecting RFC 4648 base32/base64url support.","solutions":["Use exactly 'hex' or 'base64' (lowercase) for transfer decoding","For text decoding, cast instead: pl.col('b').cast(pl.String) interprets bytes as UTF-8","For other encodings, decode to bytes first and post-process with string operations"],"exampleFix":"# before\npl.col('b').bin.decode('utf8')  # ValueError\n\n# after\npl.col('b').cast(pl.String)  # bytes -> UTF-8 text\npl.col('b').bin.decode('base64')  # valid transfer encoding","handlingStrategy":"validation","validationCode":"if encoding not in {'hex', 'base64'}:\n    raise ValueError(\"encoding must be 'hex' or 'base64'\")\nexpr = pl.col('b').bin.decode(encoding)","typeGuard":"from typing import Literal, TypeGuard\n\nTransferEncoding = Literal['hex', 'base64']\n\ndef is_transfer_encoding(v: str) -> TypeGuard[TransferEncoding]:\n    return v in ('hex', 'base64')","tryCatchPattern":null,"preventionTips":["Type the parameter Literal['hex','base64'] so typos fail statically","Remember bytes->text is .cast(pl.String), not a bin transfer decode"],"tags":["binary","encoding","validation","decode"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}