{"id":"de6365bac25d2740","repo":"aio-libs/aiohttp","slug":"body-and-text-are-not-allowed-together","errorCode":null,"errorMessage":"body and text are not allowed together","messagePattern":"body and text are not allowed together","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":555,"sourceCode":"\n    _compressed_body: bytes | None = None\n    _send_headers_immediately = False\n\n    def __init__(\n        self,\n        *,\n        body: Any = None,\n        status: int = 200,\n        reason: str | None = None,\n        text: str | None = None,\n        headers: LooseHeaders | None = None,\n        content_type: str | None = None,\n        charset: str | None = None,\n        zlib_executor_size: int = MAX_SYNC_CHUNK_SIZE,\n        zlib_executor: Executor | None = None,\n    ) -> None:\n        if body is not None and text is not None:\n            raise ValueError(\"body and text are not allowed together\")\n\n        if headers is None:\n            real_headers: CIMultiDict[str] = CIMultiDict()\n        else:\n            real_headers = CIMultiDict(headers)\n\n        if content_type is not None and \"charset\" in content_type:\n            raise ValueError(\"charset must not be in content_type argument\")\n\n        if text is not None:\n            if hdrs.CONTENT_TYPE in real_headers:\n                if content_type or charset:\n                    raise ValueError(\n                        \"passing both Content-Type header and \"\n                        \"content_type or charset params \"\n                        \"is forbidden\"\n                    )\n            else:","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L537-L573","documentation":"Response.__init__ accepts body and text as mutually exclusive kwargs. Passing both is contradictory — text is encoded into bytes and becomes the body, so supplying both is ambiguous. The check is at line 554-555.","triggerScenarios":"Calling Response(body=b'...', text='...') in the same constructor; programmatically forwarding both when wrapping values into a response; copy-paste that left both fields populated.","commonSituations":"Building a Response from a dict of optional kwargs where both got set; porting code that previously set body and now also sets text.","solutions":["Pass only one: text for str payloads (auto-encoded with charset) or body for raw bytes.","If you have a str, encode it once and pass body=, or pass text= and drop body=.","Use json_response(data=...) which handles encoding for you."],"exampleFix":"# before\nresp = Response(body=payload.encode(), text=payload)  # raises ValueError\n\n# after\nresp = Response(text=payload)","handlingStrategy":"validation","validationCode":"def make_response(*, body=None, text=None, **kw):\n    assert not (body is not None and text is not None), 'pass only one of body or text'\n    return Response(body=body, text=text, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick text= for str and body= for bytes; never both.","When forwarding optional kwargs, default unset ones to None explicitly.","Use json_response(data=...) for JSON to avoid manual encoding choices."],"tags":["http","response","constructor","validation"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}