[ perf ] Replace json-> orjson in hot path (#11221)

Signed-off-by: vincentzed <207368749+vincentzed@users.noreply.github.com>
This commit is contained in:
Vincent Zhong
2025-10-12 08:30:58 -04:00
committed by GitHub
parent 7b064f04f8
commit a220536f40
13 changed files with 32 additions and 20 deletions

View File

@@ -5,6 +5,8 @@ import logging
from abc import ABC, abstractmethod
from typing import Union
import orjson
logger = logging.getLogger(__name__)
try:
@@ -148,7 +150,7 @@ class HarmonyContext(ConversationContext):
if isinstance(tool_session, Tool):
return await tool_session.get_result(self)
tool_name = last_msg.recipient.split(".")[1]
args = json.loads(last_msg.content[0].text)
args = orjson.loads(last_msg.content[0].text)
result = await tool_session.call_tool(tool_name, args)
result_str = result.content[0].text
content = TextContent(text=result_str)

View File

@@ -7,6 +7,7 @@ import json
from collections.abc import Iterable
from typing import Literal, Optional, Union
import orjson
from openai.types.responses import (
ResponseOutputItem,
ResponseOutputMessage,
@@ -228,7 +229,7 @@ def parse_output_message(message: Message):
if len(message.content) != 1:
raise ValueError("Invalid number of contents in browser message")
content = message.content[0]
browser_call = json.loads(content.text)
browser_call = orjson.loads(content.text)
# TODO: translate to url properly!
if recipient == "browser.search":
action = ActionSearch(

View File

@@ -555,7 +555,7 @@ async def generate_request(obj: GenerateReqInput, request: Request):
async def generate_from_file_request(file: UploadFile, request: Request):
"""Handle a generate request, this is purely to work with input_embeds."""
content = await file.read()
input_embeds = json.loads(content.decode("utf-8"))
input_embeds = orjson.loads(content.decode("utf-8"))
obj = GenerateReqInput(
input_embeds=input_embeds,

View File

@@ -6,6 +6,7 @@ import uuid
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Optional, Union
import orjson
from fastapi import HTTPException, Request
from fastapi.responses import ORJSONResponse, StreamingResponse
@@ -197,7 +198,7 @@ class OpenAIServingBase(ABC):
)
try:
raw_labels = (
json.loads(raw_request.headers.get(header))
orjson.loads(raw_request.headers.get(header))
if raw_request and raw_request.headers.get(header)
else None
)

View File

@@ -7,6 +7,7 @@ import time
import uuid
from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, List, Optional, Union
import orjson
from fastapi import Request
from fastapi.responses import ORJSONResponse, StreamingResponse
from jsonschema import Draft202012Validator, SchemaError
@@ -285,7 +286,7 @@ class OpenAIServingChat(OpenAIServingBase):
if "arguments" in item["function"] and isinstance(
item["function"]["arguments"], str
):
item["function"]["arguments"] = json.loads(
item["function"]["arguments"] = orjson.loads(
item["function"]["arguments"]
)
@@ -860,7 +861,7 @@ class OpenAIServingChat(OpenAIServingBase):
finish_reason["matched"] = None
try:
# For required tool choice, we expect a JSON array of tool calls
tool_call_data = json.loads(text)
tool_call_data = orjson.loads(text)
tool_calls = []
for i, tool in enumerate(tool_call_data):
# Create a ToolCallItem from the JSON data

View File

@@ -5,7 +5,6 @@ from __future__ import annotations
import asyncio
import copy
import json
import logging
import time
from contextlib import AsyncExitStack
@@ -14,6 +13,7 @@ from typing import TYPE_CHECKING, Any, AsyncGenerator, AsyncIterator, Optional,
import jinja2
import openai.types.responses as openai_responses_types
import orjson
from fastapi import Request
from fastapi.responses import ORJSONResponse
from openai.types.responses import (
@@ -1061,7 +1061,7 @@ class OpenAIServingResponses(OpenAIServingChat):
):
function_name = previous_item.recipient[len("browser.") :]
action = None
parsed_args = json.loads(previous_item.content[0].text)
parsed_args = ororjson.loads(previous_item.content[0].text)
if function_name == "search":
action = openai_responses_types.response_function_web_search.ActionSearch(
type="search",