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%Langchain

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