-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Open
Labels
Description
Package
- langchain-openai
Reproduction Steps / Example 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))
# UnboundLocalError: cannot access local variable 'collapsed' where it is not associated with a valueError Message and Stack Trace
UnboundLocalError: cannot access local variable 'collapsed' where it is not associated with a value
Description
_consolidate_calls in _compat.py matches pairs of server_tool_call + server_tool_result items and collapses them into the Responses API format. It handles web_search, file_search, code_interpreter, remote_mcp, and mcp_list_tools by name, but if the tool name doesn't match any of these (e.g. computer, image_generation, or any future server tool), collapsed is never assigned and yield collapsed raises UnboundLocalError.
Also, line 314 uses if instead of elif for file_search, which means for every web_search call, the code unnecessarily evaluates the file_search condition and the entire elif chain that follows.
Fix
- Add a fallback branch for unrecognized tool names that emits both items unchanged (matching the existing "not a matching pair" behavior)
- Change
if current.get("name") == "file_search":toeliffor consistency with the rest of the chain
System Info
langchain-openai 0.3.22, Python 3.13
Reactions are currently unavailable