{"id":"5ed465ee6275527c","repo":"aio-libs/aiohttp","slug":"only-one-of-data-or-body-should-be-specified","errorCode":null,"errorMessage":"only one of data or body should be specified","messagePattern":"only one of data or body should be specified","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_response.py","lineNumber":782,"sourceCode":"\ndef json_bytes_response(\n    data: Any = sentinel,\n    *,\n    dumps: JSONBytesEncoder,\n    body: bytes | None = None,\n    status: int = 200,\n    reason: str | None = None,\n    headers: LooseHeaders | None = None,\n    content_type: str = \"application/json\",\n) -> Response:\n    \"\"\"Create a JSON response using a bytes-returning encoder.\n\n    Use this when your JSON encoder (like orjson) returns bytes\n    instead of str, avoiding the encode/decode overhead.\n    \"\"\"\n    if data is not sentinel:\n        if body is not None:\n            raise ValueError(\"only one of data or body should be specified\")\n        else:\n            body = dumps(data)\n    return Response(\n        body=body,\n        status=status,\n        reason=reason,\n        headers=headers,\n        content_type=content_type,\n    )\n","sourceCodeStart":764,"sourceCodeEnd":792,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_response.py#L764-L792","documentation":"json_bytes_response() is for bytes-returning encoders (e.g. orjson). It serializes `data` into body via dumps(). Passing both data= and body= is contradictory — dumps() already produces the body. The guard at lines 780-782 rejects it.","triggerScenarios":"Calling json_bytes_response(data=obj, dumps=orjson.dumps, body=b'...'). data is 'set' when not the sentinel.","commonSituations":"Migrating from json_response and leaving a body= kwarg; pre-caching serialized bytes and passing both.","solutions":["Pass only data= and let dumps() produce the bytes body.","If you already have serialized bytes and no encoder, use Response(body=bytes, content_type='application/json')."],"exampleFix":"# before\nresp = json_bytes_response(data=obj, dumps=orjson.dumps, body=orjson.dumps(obj))  # raises ValueError\n\n# after\nresp = json_bytes_response(data=obj, dumps=orjson.dumps)","handlingStrategy":"validation","validationCode":"def safe_json_bytes_response(data=sentinel, *, body=None, dumps=None, **kw):\n    if data is not sentinel and body is not None:\n        raise ValueError('pass only one of data or body')\n    from aiohttp import json_bytes_response\n    return json_bytes_response(data=data, body=body, dumps=dumps, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass data= and a dumps= callable; let it produce the body bytes.","Use Response(body=bytes, content_type='application/json') when you already have bytes and no encoder.","Avoid forwarding body= into json_bytes_response wrappers."],"tags":["http","json","response","bytes","validation"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}