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

Mistral Streaming Tool-Call Chunks With Index 0 Generate Inconsistent IDs

The `_convert_chunk_to_message_chunk` function in `langchain-mistralai` treats an index of 0 as missing due to a falsy check, causing each streamed tool-call chunk with index 0 and no id to receive a different random ID. This results in separate tool calls instead of one merged call when combining chunks.

highConfidence 95%LangchainChat ModelsAgentsChains

Origin Analysis

In `_convert_chunk_to_message_chunk`, the condition `if not raw_tool_call.get("index") and not raw_tool_call.get("id"):` uses Python truthiness, so an index value of 0 evaluates as false and triggers the random-ID fallback even though the index is a valid identifier.
1. Create two Mistral streaming tool-call chunks. The first has delta.tool_calls[0] with index=0, function.name='search', arguments='', and no id. The second has delta.tool_calls[0] with index=0, function.arguments='{"q": "weather"}', and no id. 2. Call _convert_chunk_to_message_chunk on each chunk with AIMessageChunk, then add the two resulting AIMessageChunk objects. 3. Inspect combined.tool_calls: it contains two separate entries (one with name but empty args, one with args but no name) instead of one merged call.

Fixing Code Block

Edge Case Audit

This change assumes that a tool-call index of 0 is a valid identifier even when no id is supplied. If a Mistral response ever sends multiple distinct tool calls with the same index but no id in separate chunks, they would be incorrectly merged. This has not been observed in the Mistral API. Rollback: revert to the original `not` check if this causes unexpected merging; but note that the original behavior breaks streaming tool calls with index 0. Concurrency: no shared mutable state is introduced, so thread safety is unaffected. Cross-platform: the fix is Python-version independent.

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