{"record":{"id":"3dbfe324c0b041c5","repo":"aio-libs/aiohttp","slug":"can-not-decode-content-encoding-brotli-br-plea","errorCode":null,"errorMessage":"Can not decode content-encoding: brotli (br). Please install `Brotli`","messagePattern":"Can not decode content-encoding: brotli \\(br\\)\\. Please install `Brotli`","errorType":"http","errorClass":"ContentEncodingError","httpStatus":400,"severity":"error","filePath":"aiohttp/http_parser.py","lineNumber":1149,"sourceCode":"class DeflateBuffer:\n    \"\"\"DeflateStream decompress stream and feed data into specified stream.\"\"\"\n\n    def __init__(\n        self,\n        out: StreamReader,\n        encoding: str | None,\n        max_decompress_size: int = DEFAULT_CHUNK_SIZE,\n    ) -> None:\n        self.out = out\n        self.size = 0\n        out.total_compressed_bytes = self.size\n        self.encoding = encoding\n        self._started_decoding = False\n\n        self.decompressor: BrotliDecompressor | ZLibDecompressor | ZSTDDecompressor\n        if encoding == \"br\":\n            if not HAS_BROTLI:\n                raise ContentEncodingError(\n                    \"Can not decode content-encoding: brotli (br). \"\n                    \"Please install `Brotli`\"\n                )\n            self.decompressor = BrotliDecompressor()\n        elif encoding == \"zstd\":\n            if not HAS_ZSTD:\n                raise ContentEncodingError(\n                    \"Can not decode content-encoding: zstandard (zstd). \"\n                    \"Please install `backports.zstd`\"\n                )\n            self.decompressor = ZSTDDecompressor()\n        else:\n            self.decompressor = ZLibDecompressor(encoding=encoding)\n\n        self._max_decompress_size = max_decompress_size\n\n    def set_exception(\n        self,","sourceCodeStart":1131,"sourceCodeEnd":1167,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/http_parser.py#L1131-L1167","documentation":"Raised as ContentEncodingError when a response body declares 'Content-Encoding: br' (brotli) but the optional Brotli library is not installed (HAS_BROTLI is False). DeflateBuffer.__init__ checks the flag before constructing a BrotliDecompressor and raises this actionable install hint. The error surfaces when the body is being set up for decompression.","triggerScenarios":"The server returns 'Content-Encoding: br' and auto_decompress is enabled (default), but the brotli (or Brotli) package is not importable in the aiohttp compression_utils module, so HAS_BROTLI is False.","commonSituations":"A fresh aiohttp install in a minimal environment (no brotli); a Docker image / slim deployment that omitted optional deps; CI that installs only 'aiohttp' without '[speedups]' or the brotli extra; a production env where brotli was available at build time but removed; servers that start sending brotli after an upstream change.","solutions":["Install Brotli: 'pip install Brotli' (the message names this exact package).","Or install aiohttp with the brotli extra: 'pip install \"aiohttp[brotli]\"'.","If you cannot install it, disable auto-decompression: ClientSession(..., auto_decompress=False) and decompress manually / or ask the server not to send br.","Strip 'br' from your Accept-Encoding request header so the server does not use brotli."],"exampleFix":"# before\r\npip install aiohttp\r\nsession = aiohttp.ClientSession()  # server sends br -> error\r\n\r\n# after\r\npip install Brotli\r\n# or\r\nsession = aiohttp.ClientSession(headers={'Accept-Encoding': 'gzip, deflate'})","handlingStrategy":"fallback","validationCode":"try:\n    import brotli  # noqa: F401\n    HAS_BROTLI = True\nexcept ImportError:\n    HAS_BROTLI = False\n\ndef accept_encoding_for_brotli(want_brotli: bool = True) -> str:\n    base = 'gzip, deflate'\n    if want_brotli and HAS_BROTLI:\n        return base + ', br'\n    return base","typeGuard":null,"tryCatchPattern":"from aiohttp.http_exceptions import ContentEncodingError\nimport aiohttp\n\nasync def get_robust(url):\n    try:\n        async with aiohttp.ClientSession() as s:\n            async with s.get(url) as r:\n                return await r.read()\n    except ContentEncodingError as e:\n        if 'brotli' in str(e):\n            # retry without advertising br\n            async with aiohttp.ClientSession(headers={'Accept-Encoding': 'gzip, deflate'}) as s:\n                async with s.get(url) as r:\n                    return await r.read()\n        raise","preventionTips":["Install Brotli (pip install Brotli) in the runtime image.","Advertise br in Accept-Encoding only if brotli is installed.","Use auto_decompress=False to handle decompression manually.","Pin optional deps in requirements to avoid silent removal."],"tags":["content-encoding","brotli","decompression","missing-dependency","optional-deps"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}