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

AIMessageChunk.Init_tool_calls() Fails To Merge Same-Index Continuation Tool-Call Chunks, Producing Blank Tool_calls And Invalid_tool_calls

When a single streamed delta contains multiple tool-call fragments with the same index for the same logical tool call, AIMessageChunk.tool_call_chunks retains them as separate entries. init_tool_calls() then parses them individually, resulting in an empty tool_call from the incomplete fragment and the completed fragment landing in invalid_tool_calls.

highConfidence 85%Langchain-CoreMemoryRetrieversVector StoresAffected V1.2.15Affected V1.2.26

Origin Analysis

AIMessageChunk.init_tool_calls() parses self.tool_call_chunks as-is without first coalescing multiple same-index continuation fragments that belong to the same logical tool call, unlike the merge that occurs when adding two separate AIMessageChunk objects.
Run the provided Python snippet that creates an AIMessageChunk with two same-index tool_call_chunks (one with name/search/id and one with args fragment), adds a second chunk with another args fragment, then prints tool_call_chunks, tool_calls, and invalid_tool_calls. Observe that tool_call_chunks contains both fragments, tool_calls contains a blank entry with no name or id, and invalid_tool_calls contains the valid call with the merged args.

Fixing Code Block

Edge Case Audit

This is a hotfix and may not cover all edge cases: if a single delta contains multiple non-empty distinct ids for the same index plus id=None fragments, the None fragments remain separate and may still cause issues. The patch overrides the entire init_tool_calls method and could conflict with future upstream changes. Rollback is simple: remove the monkey-patch assignment. Thoroughly test with your streaming provider before production, especially when parallel tool calls are expected.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state