AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

AIMessageChunk Tool_call_chunks Args Escapes Non-ASCII Characters

When constructing AIMessageChunk from tool_calls, the tool_call_chunks field serializes args with json.dumps using default ensure_ascii=True, causing non-ASCII characters like CJK and emojis to be stored as Unicode escape sequences instead of the original readable text.

mediumConfidence 95%Langchain-CoreAffected V<=1.4.0

Origin Analysis

In langchain_core/messages/ai.py:509, init_tool_calls back-fills tool_call_chunks by calling json.dumps(tc["args"]) without setting ensure_ascii=False, contrary to other similar serialization sites in the repo.
Run the following Python code: from langchain_core.messages import AIMessageChunk chunk = AIMessageChunk(content="", tool_calls=[{"name": "echo", "args": {"text": "你好", "emoji": "🚀"}, "id": "call_1", "type": "tool_call"}]) print(chunk.tool_call_chunks[0]["args"]) assert "你好" in chunk.tool_call_chunks[0]["args"] The assertion fails because the serialized string contains escaped Unicode sequences.

Fixing Code Block

args=json.dumps(tc["args"], ensure_ascii=False)
Replace the existing args=json.dumps(tc["args"]) at langchain_core/messages/ai.py:509 with the provided line that passes ensure_ascii=False, ensuring the serialized string preserves readable Unicode characters.

Edge Case Audit

This change only affects newly created AIMessageChunk instances; existing persisted tool_call_chunks strings containing escaped Unicode sequences will not be automatically converted. If downstream code relies on ASCII-safe serialization for storage or log safety, this may introduce raw Unicode into databases or logs that had previously only contained escaped output. Test integration with langgraph checkpointers and langsmith traces. Rollback: revert this single-line change if any storage encoding issues arise.

Ecosystem Topology