From 006a63e641bcf655ea488f1fbf670de1bd7aa4dd Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Fri, 31 Jul 2026 23:26:07 +0200 Subject: [PATCH] perf: use the orjson codec for the Anthropic passthrough request body (#27810) `passthrough_anthropic_messages` in `main.py` serializes the full request payload with stdlib `json` before sending it upstream. It is the largest single serialization on that path, since the body carries the whole conversation. It now goes through `JSONCodec`, which selects orjson when `ENABLE_ORJSON` is set. The result is passed to aiohttp as `data=`, which encodes `str` as UTF-8 and sets `Content-Length` from the encoded bytes. The payload originates from a parsed request dict, so it holds only JSON-native types, and the serialized string is never hashed, compared or persisted. The remaining stdlib `json` calls in this module are left alone: two are a debug log line and a fixed Ollama unload payload, and one parses an upstream error body. With `ENABLE_ORJSON` unset, which is the default, `JSONCodec` is stdlib `json` and this call site behaves exactly as before. --- backend/open_webui/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 47fbd7a73c..d4f72a3632 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -229,6 +229,7 @@ from open_webui.utils.chat_variables import ( normalize_chat_variables, ) from open_webui.utils.embeddings import generate_embeddings +from open_webui.utils.json_codec import JSONCodec from open_webui.utils.json_response import apply_orjson_http_json from open_webui.utils.logger import start_logger from open_webui.utils.middleware import ( @@ -1842,7 +1843,7 @@ async def passthrough_anthropic_messages(request: Request, form_data: dict, user response = await session.request( method='POST', url=request_url, - data=json.dumps(payload), + data=JSONCodec.dumps(payload), headers=headers, cookies=cookies, ssl=AIOHTTP_CLIENT_SESSION_SSL,