Skip to content

fix(openai): handle unrecognized server tool names in _consolidate_calls#36187

Open
Yifei Leng (Senna1960321) wants to merge 1 commit intolangchain-ai:masterfrom
Senna1960321:fix/consolidate-calls-unbound-local
Open

fix(openai): handle unrecognized server tool names in _consolidate_calls#36187
Yifei Leng (Senna1960321) wants to merge 1 commit intolangchain-ai:masterfrom
Senna1960321:fix/consolidate-calls-unbound-local

Conversation

@Senna1960321
Copy link
Copy Markdown

Summary

Fixes #36140

_consolidate_calls crashes with UnboundLocalError when a server_tool_call has a name not in the known set (web_search, file_search, code_interpreter, remote_mcp, mcp_list_tools). This affects any new or unrecognized server tool types (e.g. computer, image_generation) during v1 → Responses API format conversion.

Changes:

  • Add fallback else branch for unrecognized tool names that emits both items unchanged (matching the existing "not a matching pair" behavior)
  • Change second if to elif for file_search to avoid redundant condition evaluation on every web_search call

Repro before fix:

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

Review notes

The fix is intentionally minimal: unrecognized tools pass through unchanged rather than attempting a best-effort collapse, since we don't know the target schema for unknown tool types.

AI Disclosure

This bug was identified through code review with AI assistance.

When _consolidate_calls encounters a server_tool_call with a name not in
the known list (web_search, file_search, code_interpreter, remote_mcp,
mcp_list_tools), the collapsed variable is never assigned, causing
UnboundLocalError on yield. This affects any new or unrecognized server
tool types during v1 → Responses API format conversion.

Fix:
- Add fallback branch for unrecognized names that emits both items unchanged
- Change second `if` to `elif` for file_search to avoid redundant evaluation

Fixes langchain-ai#36140
@github-actions github-actions bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs size: S 50-199 LOC labels Mar 24, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 24, 2026

Merging this PR will degrade performance by 12.75%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

❌ 1 regressed benchmark
✅ 2 untouched benchmarks
⏩ 37 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_init_time 121.5 ms 139.3 ms -12.75%

Comparing Senna1960321:fix/consolidate-calls-unbound-local (fb09dc2) with master (1778b08)

Open in CodSpeed

Footnotes

  1. 37 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration new-contributor openai `langchain-openai` package issues & PRs size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant