{"record":{"id":"be73b91ca54331ab","repo":"aio-libs/aiohttp","slug":"compress-must-be-one-of-true-false-deflate-or","errorCode":null,"errorMessage":"compress must be one of True, False, 'deflate', or 'gzip'","messagePattern":"compress must be one of True, False, 'deflate', or 'gzip'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_reqrep.py","lineNumber":1215,"sourceCode":"\n        self.headers[hdrs.COOKIE] = c.output(header=\"\", sep=\";\").strip()\n\n    def _update_content_encoding(\n        self, data: Any, compress: bool | Literal[\"deflate\", \"gzip\"]\n    ) -> None:\n        \"\"\"Set request content encoding.\"\"\"\n        self.compress = None\n        if not data:\n            return\n\n        if self.headers.get(hdrs.CONTENT_ENCODING):\n            if compress:\n                raise ValueError(\n                    \"compress can not be set if Content-Encoding header is set\"\n                )\n        elif compress:\n            if isinstance(compress, str) and compress not in {\"deflate\", \"gzip\"}:\n                raise ValueError(\n                    \"compress must be one of True, False, 'deflate', or 'gzip'\"\n                )\n            self.compress = compress if isinstance(compress, str) else \"deflate\"\n            self.headers[hdrs.CONTENT_ENCODING] = self.compress\n            self.chunked = True  # enable chunked, no need to deal with length\n\n    def _update_transfer_encoding(self) -> None:\n        \"\"\"Analyze transfer-encoding header.\"\"\"\n        te = self.headers.get(hdrs.TRANSFER_ENCODING, \"\").lower()\n\n        if \"chunked\" in te:\n            if self.chunked:\n                raise ValueError(\n                    \"chunked can not be set \"\n                    'if \"Transfer-Encoding: chunked\" header is set'\n                )\n\n        elif self.chunked:","sourceCodeStart":1197,"sourceCodeEnd":1233,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/client_reqrep.py#L1197-L1233","documentation":"Raised by ClientRequest._update_content_encoding() when the 'compress' parameter is a string that is neither 'deflate' nor 'gzip'. aiohttp supports only those two algorithms for request body compression; any other string value (e.g. 'br', 'zstd') is rejected with ValueError. Non-string truthy values (True) default to 'deflate' and do not trigger this.","triggerScenarios":"Calling session.post(url, data=body, compress='br') or compress='zstd'. The isinstance(compress, str) and not-in-{'deflate','gzip'} check fails and raises ValueError.","commonSituations":"Assuming Brotli/zstd (supported for response decoding) is also supported for request compression; passing a compression name from config without validation; case mismatch ('GZIP' vs 'gzip').","solutions":["Use one of the supported values: compress=True (deflate), compress='deflate', or compress='gzip'.","Note values are case-sensitive — use lowercase 'gzip'/'deflate'.","If you need Brotli/zstd for requests, compress the body yourself and set Content-Encoding manually (then don't pass compress).","Validate config-driven compress strings against {'deflate','gzip'}."],"exampleFix":"# before\nawait session.post(url, data=body, compress='br')  # ValueError\n\n# after\nawait session.post(url, data=body, compress='gzip')","handlingStrategy":"type-guard","validationCode":"def normalize_compress(compress):\n    if compress is True:\n        return 'deflate'\n    if compress is False or compress is None:\n        return None\n    if isinstance(compress, str) and compress.lower() in ('deflate', 'gzip'):\n        return compress.lower()\n    raise ValueError(f\"compress must be True, False, 'deflate', or 'gzip', got {compress!r}\")","typeGuard":"def is_supported_compress(compress) -> bool:\n    return compress in (True, False, None) or (isinstance(compress, str) and compress.lower() in ('deflate', 'gzip'))","tryCatchPattern":"try:\n    await session.post(url, data=body, compress=compress)\nexcept ValueError as e:\n    if 'compress must be one of' in str(e):\n        await session.post(url, data=body, compress='gzip')  # safe default\n    else:\n        raise","preventionTips":["Restrict compress to the literal set {True, False, 'deflate', 'gzip'}.","Normalize case to lowercase before passing string values.","Use Brotli/zstd only via manual body compression + Content-Encoding."],"tags":["client","request","compression","validation","configuration"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}