{"record":{"id":"1eb90a30bb35c5f0","repo":"aio-libs/aiohttp","slug":"you-can-t-enable-chunked-encoding-when-a-content-l","errorCode":null,"errorMessage":"You can't enable chunked encoding when a content length is set","messagePattern":"You can't enable chunked encoding when a content length is set","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":177,"sourceCode":"        elif \"\\r\" in reason or \"\\n\" in reason:\n            raise ValueError(\"Reason cannot contain \\\\r or \\\\n\")\n        self._reason = reason\n\n    @property\n    def keep_alive(self) -> bool | None:\n        return self._keep_alive\n\n    def force_close(self) -> None:\n        self._keep_alive = False\n\n    @property\n    def body_length(self) -> int:\n        return self._body_length\n\n    def enable_chunked_encoding(self) -> None:\n        \"\"\"Enables automatic chunked transfer encoding.\"\"\"\n        if hdrs.CONTENT_LENGTH in self._headers:\n            raise RuntimeError(\n                \"You can't enable chunked encoding when a content length is set\"\n            )\n        self._chunked = True\n\n    def enable_compression(\n        self,\n        force: ContentCoding | None = None,\n        strategy: int | None = None,\n    ) -> None:\n        \"\"\"Enables response compression encoding.\"\"\"\n        # Don't enable compression if content is already encoded.\n        # This prevents double compression and provides a safe, predictable behavior\n        # without breaking existing code that may call enable_compression() on\n        # responses that already have Content-Encoding set (e.g., FileResponse\n        # serving pre-compressed files).\n        if hdrs.CONTENT_ENCODING in self._headers:\n            return\n        self._compression = True","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_response.py#L159-L195","documentation":"StreamResponse.enable_chunked_encoding() refuses to enable chunked transfer if a Content-Length header is already present. The two are mutually exclusive under HTTP/1.1: a response carries either a fixed length or chunked framing, never both.","triggerScenarios":"Setting resp.content_length = N (or constructing Response(content_length=N)) and then calling resp.enable_chunked_encoding().","commonSituations":"Streaming response where length was set first by mistake; middleware adding Content-Length before the handler enables chunked; copy-paste from a fixed-length template.","solutions":["Remove the Content-Length header before enabling chunked: resp.headers.pop(hdrs.CONTENT_LENGTH, None).","Choose one transfer mode per response — never set both.","Avoid setting content_length on a response you intend to stream."],"exampleFix":"# before\nresp.content_length = size\nresp.enable_chunked_encoding()\n# after\nresp.headers.pop('Content-Length', None)\nresp.enable_chunked_encoding()","handlingStrategy":"validation","validationCode":"from aiohttp import hdrs\nif hdrs.CONTENT_LENGTH in resp.headers:\n    del resp.headers[hdrs.CONTENT_LENGTH]\nresp.enable_chunked_encoding()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide transfer mode (fixed length vs chunked) before constructing the response.","Never set content_length on a response you will stream.","Remove Content-Length before enabling chunked if both ended up set."],"tags":["response","chunked","content-length","transfer-encoding"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}