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

Bug: Streaming Tool Call Fires Prematurely When First SSE Chunk Has Empty Args

When streaming tool calls from providers like OpenRouter or deepseek-v3.2, the first SSE chunk may contain an empty args string. AIMessageChunk incorrectly treats this as a complete tool call with empty arguments, causing agents to invoke the tool prematurely and often crash with ValidationError.

highConfidence 85%LangChainAffected V0.3.x

Origin Analysis

In langchain_core.messages.ai.AIMessageChunk._init_tool_calls, the logic for parsing args treats an empty string (args_str == '') the same as None, both resulting in args = {}. This causes the tool call to be created immediately after the first chunk, before actual arguments have been streamed.
1. Import AIMessageChunk from langchain_core.messages.\n2. Create an AIMessageChunk with tool_call_chunks=[{'name': 'get_weather', 'args': '', 'id': 'call_abc', 'index': 0}].\n3. Access the tool_calls property.\n4. Observe that it returns [{'name': 'get_weather', 'args': {}, 'id': 'call_abc', 'type': 'tool_call'}] instead of [].

Fixing Code Block

Edge Case Audit

This fix may drop tool calls that legitimately have no arguments if the provider sends an empty string instead of '{}'. Providers should send '{}' for no-argument tools. Additionally, partial JSON strings that are non-empty but invalid are still coerced to {}, which could cause issues for fragmented streams with incomplete JSON in intermediate chunks. Rollback: remove the early continue to revert to previous behavior. Test thoroughly with providers that may send empty args for no-arg tools.

Ecosystem Topology