aio-libs/aiohttp · error · ValueError
compress must be one of True, False, 'deflate', or 'gzip'
Error message
compress must be one of True, False, 'deflate', or 'gzip'
What it means
Raised as ValueError in ClientRequest._update_content_encoding() when `compress` is a string but not one of the two supported compression algorithms. aiohttp accepts True (defaults to deflate), False/None, or the literal strings 'deflate' and 'gzip'; anything else is rejected.
Source
Thrown at aiohttp/client_reqrep.py:1215
self.headers[hdrs.COOKIE] = c.output(header="", sep=";").strip()
def _update_content_encoding(
self, data: Any, compress: bool | Literal["deflate", "gzip"]
) -> None:
"""Set request content encoding."""
self.compress = None
if not data:
return
if self.headers.get(hdrs.CONTENT_ENCODING):
if compress:
raise ValueError(
"compress can not be set if Content-Encoding header is set"
)
elif compress:
if isinstance(compress, str) and compress not in {"deflate", "gzip"}:
raise ValueError(
"compress must be one of True, False, 'deflate', or 'gzip'"
)
self.compress = compress if isinstance(compress, str) else "deflate"
self.headers[hdrs.CONTENT_ENCODING] = self.compress
self.chunked = True # enable chunked, no need to deal with length
def _update_transfer_encoding(self) -> None:
"""Analyze transfer-encoding header."""
te = self.headers.get(hdrs.TRANSFER_ENCODING, "").lower()
if "chunked" in te:
if self.chunked:
raise ValueError(
"chunked can not be set "
'if "Transfer-Encoding: chunked" header is set'
)
elif self.chunked:View on GitHub (pinned to c0ef574e29)
Solutions
- Use one of the two supported lowercase strings: 'gzip' or 'deflate'.
- Use True to get the default (deflate).
- For brotli/zstd on the request side, pre-encode the body manually and set Content-Encoding yourself (see error 72).
- Strip and lowercase dynamic compress values before passing.
Example fix
# before
await session.post(url, data=body, compress='br')
# after
await session.post(url, data=body, compress='gzip')
# or pre-encode for unsupported algorithms
encoded = brotli.compress(body)
await session.post(url, data=encoded,
headers={'Content-Encoding': 'br'}) Defensive patterns
Strategy: validation
Validate before calling
if isinstance(compress, str) and compress not in {'deflate', 'gzip'}:
raise ValueError(f"unsupported compress {compress!r}; use 'gzip' or 'deflate'") Type guard
def is_valid_compress(c) -> bool:
return c in (True, False, None) or c in {'deflate', 'gzip'} Prevention
- Restrict compress to the supported set.
- Lowercase and strip dynamic values.
- For brotli/zstd, pre-encode the body and set Content-Encoding manually.
When it happens
Trigger: Fires at line 1214-1217 when compress is a string not in {'deflate','gzip'}. Triggered by compress='br' (brotli), compress='zstd', compress='gzip ' (trailing space), or typos like compress='zip'.
Common situations: Assuming aiohttp supports the same encodings as the Accept-Encoding list (brotli/zstd); trailing whitespace; case mismatches ('GZIP' uppercase may slip through depending on version but is not guaranteed).
Related errors
- compress can not be set if Content-Encoding header is set
- Compress wbits must between 9 and 15, zlib does not support
- Method cannot contain non-token characters {method!r} (found
- Invalid Content-Length header: {content_length_hdr!r}
- chunked can not be set if "Transfer-Encoding: chunked" heade
AI-assisted analysis of aio-libs/aiohttp@c0ef574e29 (2026-08-04).
Data as JSON: /data/errors/be73b91ca54331ab.json.
Report an issue: GitHub.