Merge pull request #3457 from ahoshaiyan/fix/reduce-tool-result-context-size

Reduce Tool Result Context Size by Using UTF-8 for JSON Serialization
This commit is contained in:
Mark Backman
2026-03-12 10:41:33 -04:00
committed by GitHub
9 changed files with 13 additions and 8 deletions

View File

@@ -0,0 +1 @@
- Changed tool result JSON serialization to use `ensure_ascii=False`, preserving UTF-8 characters instead of escaping them. This reduces context size and token usage for non-English languages.

View File

@@ -1246,7 +1246,7 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator):
frame: Frame containing function call result.
"""
if frame.result:
result = json.dumps(frame.result)
result = json.dumps(frame.result, ensure_ascii=False)
await self._update_function_call_result(frame.function_name, frame.tool_call_id, result)
else:
await self._update_function_call_result(

View File

@@ -691,7 +691,7 @@ class AWSBedrockAssistantContextAggregator(LLMAssistantContextAggregator):
frame: The function call result frame to handle.
"""
if frame.result:
result = json.dumps(frame.result)
result = json.dumps(frame.result, ensure_ascii=False)
await self._update_function_call_result(frame.function_name, frame.tool_call_id, result)
else:
await self._update_function_call_result(

View File

@@ -1044,7 +1044,9 @@ class AWSNovaSonicLLMService(LLMService):
"toolResult": {
"promptName": self._prompt_name,
"contentName": content_name,
"content": json.dumps(result) if isinstance(result, dict) else result,
"content": json.dumps(result, ensure_ascii=False)
if isinstance(result, dict)
else result,
}
}
}

View File

@@ -200,7 +200,9 @@ class GoogleAssistantContextAggregator(OpenAIAssistantContextAggregator):
if message.role == "user":
for part in message.parts:
if part.function_response and part.function_response.id == tool_call_id:
part.function_response.response = {"value": json.dumps(result)}
part.function_response.response = {
"value": json.dumps(result, ensure_ascii=False)
}
@dataclass

View File

@@ -939,7 +939,7 @@ class GrokRealtimeLLMService(LLMService):
item = events.ConversationItem(
type="function_call_output",
call_id=tool_call_id,
output=json.dumps(result),
output=json.dumps(result, ensure_ascii=False),
)
await self.send_client_event(events.ConversationItemCreateEvent(item=item))

View File

@@ -255,7 +255,7 @@ class OpenAIAssistantContextAggregator(LLMAssistantContextAggregator):
frame: Frame containing the function call result.
"""
if frame.result:
result = json.dumps(frame.result)
result = json.dumps(frame.result, ensure_ascii=False)
await self._update_function_call_result(frame.function_name, frame.tool_call_id, result)
else:
await self._update_function_call_result(

View File

@@ -1128,7 +1128,7 @@ class OpenAIRealtimeLLMService(LLMService):
item = events.ConversationItem(
type="function_call_output",
call_id=tool_call_id,
output=json.dumps(result),
output=json.dumps(result, ensure_ascii=False),
)
await self.send_client_event(events.ConversationItemCreateEvent(item=item))

View File

@@ -441,7 +441,7 @@ class OpenAIRealtimeBetaLLMService(LLMService):
item = events.ConversationItem(
type="function_call_output",
call_id=frame.tool_call_id,
output=json.dumps(frame.result),
output=json.dumps(frame.result, ensure_ascii=False),
)
await self.send_client_event(events.ConversationItemCreateEvent(item=item))