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

Openai: Malformed Tool Calls Cause All Tool Calls To Be Silently Dropped

A malformed tool call entry (missing 'function' or 'index') in a streaming OpenAI response causes _convert_delta_to_message_chunk to silently discard all tool call chunks due to a broad except KeyError: pass around a list comprehension.

highConfidence 95%LangchainAffected V<=1.1.11

Origin Analysis

In langchain_openai/chat_models/base.py, the _convert_delta_to_message_chunk function wraps a list comprehension for raw_tool_calls inside a single try/except KeyError: pass. When any tool call delta lacks the required 'function' or 'index' key, the KeyError aborts the entire comprehension, and the exception is swallowed, resulting in zero tool call chunks even for valid entries.
Run the provided minimal reproduction with langchain-openai 1.1.11 and langchain-core 1.2.18. Call _convert_delta_to_message_chunk with a delta dict containing two tool_call entries: one valid with 'function' and 'index', the other missing 'function'. Observe that result.tool_call_chunks is empty instead of containing the valid chunk.

Fixing Code Block

Edge Case Audit

The per-item skip introduces silent per-item failure without logging. This may hide upstream API contract violations and make debugging harder. Downstream code that previously received no tool calls when any one was malformed now receives partial valid tool calls; verify consumers handle partial sets correctly. Recommend adding a warning log inside the except block and considering a metric for malformed deltas. Rollback: revert to the original block if unexpected downstream behavior occurs. Apply the same pattern to any async or duplicated code paths.

Ecosystem Topology