{"id":"b20e27e61407c392","repo":"aio-libs/aiohttp","slug":"you-can-t-set-content-length-when-chunked-encoding","errorCode":null,"errorMessage":"You can't set content length when chunked encoding is enable","messagePattern":"You can't set content length when chunked encoding is enable","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":213,"sourceCode":"        self._compression = True\n        self._compression_force = force\n        self._compression_strategy = strategy\n\n    @property\n    def headers(self) -> \"CIMultiDict[str]\":\n        return self._headers\n\n    @property\n    def content_length(self) -> int | None:\n        # Just a placeholder for adding setter\n        return super().content_length\n\n    @content_length.setter\n    def content_length(self, value: int | None) -> None:\n        if value is not None:\n            value = int(value)\n            if self._chunked:\n                raise RuntimeError(\n                    \"You can't set content length when chunked encoding is enable\"\n                )\n            self._headers[hdrs.CONTENT_LENGTH] = str(value)\n        else:\n            self._headers.pop(hdrs.CONTENT_LENGTH, None)\n\n    @property\n    def content_type(self) -> str:\n        # Just a placeholder for adding setter\n        return super().content_type\n\n    @content_type.setter\n    def content_type(self, value: str) -> None:\n        self.content_type  # read header values if needed\n        self._content_type = str(value)\n        self._generate_content_type_header()\n\n    @property","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L195-L231","documentation":"Raised as RuntimeError by StreamResponse.content_length setter (aiohttp/web_response.py:213) when self._chunked is already True. This is the symmetric counterpart to error 218: once Transfer-Encoding: chunked has been enabled, you cannot also set Content-Length because HTTP forbids mixing the two framing styles.","triggerScenarios":"Calling resp.enable_chunked_encoding() first, then assigning resp.content_length = N. Also triggered by code paths that auto-set content_length (e.g. after writing a known-size body) on a response that was already put into chunked mode.","commonSituations":"Middleware that forces chunked encoding on all responses conflicting with code that fixes a Content-Length; refactoring a handler from fixed-length to streaming but leaving the content_length assignment; copy-pasting code that mixed both patterns.","solutions":["Pick one framing strategy and stick with it for the lifetime of the response.","If you previously called enable_chunked_encoding(), do not set content_length afterwards - chunked bodies have unknown length by definition.","Reset chunked mode (construct a new StreamResponse) if you genuinely need to switch back to a fixed length."],"exampleFix":"// before\nresp = web.StreamResponse()\nresp.enable_chunked_encoding()\nresp.content_length = 100  # RuntimeError\n\n\n# after (option A: fixed length)\nresp = web.StreamResponse()\nresp.content_length = 100\n\n# after (option B: chunked, no length)\nresp = web.StreamResponse()\nresp.enable_chunked_encoding()","handlingStrategy":"validation","validationCode":"if not resp._chunked:\n    resp.content_length = 100","typeGuard":"def can_set_content_length(resp: web.StreamResponse) -> bool:\n    return not getattr(resp, \"_chunked\", False)","tryCatchPattern":"try:\n    resp.content_length = 100\nexcept RuntimeError:\n    # response is chunked; construct a new non-chunked response instead\n    raise","preventionTips":["Never set content_length after enable_chunked_encoding().","Pick one framing strategy and stick with it for the response lifetime.","Construct a new StreamResponse if you genuinely need to switch back."],"tags":["http","response","chunked","framing"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}