Skip to content

langchain-openai: _consolidate_calls raises UnboundLocalError for unrecognized server tool names #36140

@Senna1960321

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 value

Error 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

  1. Add a fallback branch for unrecognized tool names that emits both items unchanged (matching the existing "not a matching pair" behavior)
  2. Change if current.get("name") == "file_search": to elif for consistency with the rest of the chain

System Info

langchain-openai 0.3.22, Python 3.13

Metadata

Metadata

Labels

externalopenai`langchain-openai` package issues & PRs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions