{"record":{"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/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_response.py#L195-L231","documentation":"The StreamResponse.content_length setter refuses to assign a value when _chunked is already True — the inverse of error 218. Once a response is committed to chunked transfer you cannot switch it back to a fixed Content-Length.","triggerScenarios":"resp.enable_chunked_encoding() followed by resp.content_length = N.","commonSituations":"Streaming setup followed by an attempt to set length (e.g., for a HEAD request or after computing the size); copy-paste from a fixed-length handler into a chunked one.","solutions":["Set content_length BEFORE calling enable_chunked_encoding().","Do not mix chunked and fixed-length on the same response.","If you must reset, drop the Content-Length header instead of toggling modes."],"exampleFix":"# before\nresp.enable_chunked_encoding()\nresp.content_length = size\n# after\nresp.content_length = size   # do NOT enable chunked on this response","handlingStrategy":"validation","validationCode":"if resp._chunked:\n    raise RuntimeError('response already chunked')\nresp.content_length = size","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set content_length BEFORE enable_chunked_encoding().","Pick one transfer mode per response and stick to it.","Do not toggle chunked on then try to assign a fixed length."],"tags":["response","content-length","chunked","transfer-encoding"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}