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

Langchain-Openai: _Consolidate_calls Raises UnboundLocalError For Unrecognized Server Tool Names

The _consolidate_calls function in langchain_openai.chat_models._compat fails with UnboundLocalError when processing a server_tool_call whose name is not one of the recognized built-in tools (e.g., computer, image_generation). Additionally, a minor control flow issue causes unnecessary evaluation of the file_search condition for web_search calls.

highConfidence 85%LangchainAgentsVector StoresAffected V0.3.22

Origin Analysis

The local variable 'collapsed' is only assigned inside known tool-name branches; when an unrecognized tool name occurs, no branch executes, leading to an unbound local variable reference at 'yield collapsed'.
Run the following Python code: ```python from langchain_openai.chat_models._compat import _consolidate_calls items = [ {"type": "server_tool_call", "name": "computer", "id": "call_1"}, {"type": "server_tool_result", "tool_call_id": "call_1", "output": "result"}, ] list(_consolidate_calls(items)) ``` Observed: UnboundLocalError: cannot access local variable 'collapsed' where it is not associated with a value.

Fixing Code Block

Edge Case Audit

The fallback branch yields the original call and result items rather than a collapsed Responses API object, which may cause downstream components expecting only collapsed objects to fail. Test with known tools and ensure that any consumer handling these yields can accept the raw items. The change from 'if' to 'elif' is behavior-neutral but alters subtle branching; verify that no tool name falls through multiple conditions unintentionally. Rollback: revert to original code and temporarily filter out unsupported tool names before calling _consolidate_calls.

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