Skip to content

fix: add ensure_ascii=False to json.dumps for correct Unicode output #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/agents/extensions/models/litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ async def get_response(
logger.debug("Received model response")
else:
logger.debug(
f"LLM resp:\n{json.dumps(response.choices[0].message.model_dump(), indent=2)}\n"
f"LLM resp:\n{json.dumps(
response.choices[0].message.model_dump(),
indent=2,
ensure_ascii=False
)}\n"
)

if hasattr(response, "usage"):
Expand Down Expand Up @@ -251,8 +255,8 @@ async def _fetch_response(
else:
logger.debug(
f"Calling Litellm model: {self.model}\n"
f"{json.dumps(converted_messages, indent=2)}\n"
f"Tools:\n{json.dumps(converted_tools, indent=2)}\n"
f"{json.dumps(converted_messages, indent=2, ensure_ascii=False)}\n"
f"Tools:\n{json.dumps(converted_tools, indent=2, ensure_ascii=False)}\n"
f"Stream: {stream}\n"
f"Tool choice: {tool_choice}\n"
f"Response format: {response_format}\n"
Expand Down
5 changes: 4 additions & 1 deletion src/agents/mcp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ async def invoke_mcp_tool(
if len(result.content) == 1:
tool_output = result.content[0].model_dump_json()
elif len(result.content) > 1:
tool_output = json.dumps([item.model_dump() for item in result.content])
tool_output = json.dumps(
[item.model_dump() for item in result.content],
ensure_ascii=False
)
else:
logger.error(f"Errored MCP tool result: {result}")
tool_output = "Error running tool."
Expand Down
3 changes: 2 additions & 1 deletion src/agents/models/chatcmpl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
{
"queries": file_search.get("queries", []),
"status": file_search.get("status"),
}
},
ensure_ascii=False
),
},
)
Expand Down
11 changes: 8 additions & 3 deletions src/agents/models/openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ async def get_response(
logger.debug("Received model response")
else:
logger.debug(
f"LLM resp:\n{json.dumps(response.choices[0].message.model_dump(), indent=2)}\n"
"LLM resp:\n",
f"""{json.dumps(
response.choices[0].message.model_dump(),
indent=2,
ensure_ascii=False
)}\n"""
)

usage = (
Expand Down Expand Up @@ -222,8 +227,8 @@ async def _fetch_response(
logger.debug("Calling LLM")
else:
logger.debug(
f"{json.dumps(converted_messages, indent=2)}\n"
f"Tools:\n{json.dumps(converted_tools, indent=2)}\n"
f"{json.dumps(converted_messages, indent=2, ensure_ascii=False)}\n"
f"Tools:\n{json.dumps(converted_tools, indent=2, ensure_ascii=False)}\n"
f"Stream: {stream}\n"
f"Tool choice: {tool_choice}\n"
f"Response format: {response_format}\n"
Expand Down
10 changes: 7 additions & 3 deletions src/agents/models/openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ async def get_response(
else:
logger.debug(
"LLM resp:\n"
f"{json.dumps([x.model_dump() for x in response.output], indent=2)}\n"
f"""{json.dumps(
[x.model_dump() for x in response.output],
indent=2,
ensure_ascii=False
)}\n"""
)

usage = (
Expand Down Expand Up @@ -231,8 +235,8 @@ async def _fetch_response(
else:
logger.debug(
f"Calling LLM {self.model} with input:\n"
f"{json.dumps(list_input, indent=2)}\n"
f"Tools:\n{json.dumps(converted_tools.tools, indent=2)}\n"
f"{json.dumps(list_input, indent=2, ensure_ascii=False)}\n"
f"Tools:\n{json.dumps(converted_tools.tools, indent=2, ensure_ascii=False)}\n"
f"Stream: {stream}\n"
f"Tool choice: {tool_choice}\n"
f"Response format: {response_format}\n"
Expand Down