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

Core: Finalize_tool_call_chunk Coerces Id=None To Empty String, Collapsing Parallel Id-Less Tool Calls

langchain-core's finalize_tool_call_chunk maps missing tool_call id (None) to "" in the tool_call and server_tool_call branches, causing multiple id-less parallel tool calls to share the same empty id and collide downstream (e.g., deepagents FilesystemMiddleware offloads all to /large_tool_results/unknown). The proposed fix replaces the empty-string fallback with uuid4().hex to guarantee distinct ids.

highConfidence 93%LangchainAffected V<=1.6.3

Origin Analysis

The compat bridge (libs/core/langchain_core/language_models/_compat_bridge.py) normalizes id_=None to id="" via `id=id_ or ""` in finalize_tool_call_chunk. This violates the ToolCall.id type contract (str | None), conflates 'absent id' with 'empty string', and makes every id-less tool call indistinguishable from others with the same name/args.
Run the provided MRE: create two ChatGenerationChunk with AIMessageChunk containing tool_call_chunks with id=None, indices 0 and 1. Pass them to chunks_to_events and dispatch to ChatModelStream. Print tool_calls ids; both show id='', proving collapse.

Fixing Code Block

Edge Case Audit

Potential secondary issue: if a single logical tool call is streamed in multiple chunks and some chunks carry the provider id while others do not, the fallback will generate a new UUID for each id-less chunk, causing the same logical call to fragment into multiple tool calls during aggregation. This is especially relevant for providers that intermittently omit the id field. Mitigation: ensure the streaming/provider layer assigns a stable id per call before the compat bridge, or track seen calls by index/name/args when id is missing. Rollback: reverting to `id_ or ""` restores prior behavior but reintroduces the collision; it may be acceptable if downstream handles empty ids explicitly. Also align the two dead fallback sites in chat_model_stream.py (`.get("id", "")`) to avoid confusion.

Ecosystem Topology