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

Fix Broken If/Elif Chain And UnboundLocalError In _Consolidate_calls

The _consolidate_calls function in langchain_openai.chat_models._compat uses a bare if for file_search after web_search, breaking the elif chain and causing the else branch to attach to file_search. Unknown server_tool_call types hit else: pass, leaving collapsed unassigned and raising UnboundLocalError.

highConfidence 85%Langchain-Openailangchain-coreVector StoresAgents

Origin Analysis

The file_search branch uses a bare `if` instead of `elif`, so it is evaluated independently of the preceding web_search branch. This causes the subsequent `elif`/`else` to attach to the file_search `if` rather than the original dispatch chain. For unknown tool types, the `else: pass` executes without assigning `collapsed`, leading to UnboundLocalError when `yield collapsed` is reached.
1. Use langchain-openai with a model response that includes a server tool call with an unknown type or name. 2. Observe an UnboundLocalError when the response is processed by _consolidate_calls. 3. Alternatively, include both web_search and file_search tool calls and observe that file_search overwrites collapsed, producing incorrect output.

Fixing Code Block

Edge Case Audit

Skipping unknown tool calls may hide newer OpenAI tool types and silently drop them from the output. Add a log or forward unknown types if forward compatibility is required. Rollback by reverting `continue` to `pass` and `elif` back to `if`, but then initialize `collapsed = None` before the chain or only yield when `collapsed` is assigned to avoid UnboundLocalError.

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