langchain - ✅(Solved) Fix Bug: _consolidate_calls if/elif chain broken and UnboundLocalError on unknown tool names [4 pull requests, 6 comments, 5 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35694Fetched 2026-04-08 00:25:05
View on GitHub
Comments
6
Participants
5
Timeline
13
Reactions
0
Timeline (top)
commented ×6cross-referenced ×4referenced ×2labeled ×1

Two issues in _consolidate_calls:

  1. the file_search branch uses a bare if instead of elif, so it's evaluated even after web_search already matched — and the downstream elif/else chain attaches to file_search rather than the full dispatch.

  2. when a server_tool_call has a name that doesn't match any known tool (web_search, file_search, code_interpreter, etc.), the else branch does pass and falls through to yield collapsed where collapsed was never assigned, causing an UnboundLocalError.

Root Cause

Two issues in _consolidate_calls:

  1. the file_search branch uses a bare if instead of elif, so it's evaluated even after web_search already matched — and the downstream elif/else chain attaches to file_search rather than the full dispatch.

  2. when a server_tool_call has a name that doesn't match any known tool (web_search, file_search, code_interpreter, etc.), the else branch does pass and falls through to yield collapsed where collapsed was never assigned, causing an UnboundLocalError.

Fix Action

Fix / Workaround

  1. the file_search branch uses a bare if instead of elif, so it's evaluated even after web_search already matched — and the downstream elif/else chain attaches to file_search rather than the full dispatch.

PR fix notes

PR #35482: fix(openai): fix if/elif chain and unhandled tool name in _consolidate_calls

Description (problem / solution / changelog)

problem

_consolidate_calls in _compat.py has two issues:

  1. the file_search branch uses a bare if instead of elif, breaking the mutually exclusive chain that starts with web_search. functionally this doesn't cause wrong results today since a tool name can only match one branch, but it means the file_search condition is evaluated even after web_search already matched — and more importantly, the downstream elif/else chain is attached to file_search rather than the full dispatch.

  2. when a server_tool_call has a name that doesn't match any known tool (web_search, file_search, code_interpreter, remote_mcp, mcp_list_tools), the else branch does pass but the code still falls through to yield collapsed — where collapsed was never assigned, causing an UnboundLocalError. this can happen if openai adds new server tool types in the future.

fix

  • change file_search's if to elif so the entire dispatch is a single if/elif/else chain
  • in the else branch, emit both the call and result items unchanged instead of silently dropping them

tests

added two tests:

  • test_consolidate_calls_unknown_tool_name: verifies that unrecognized tool names pass through both items without error
  • test_consolidate_calls_web_search: verifies that web_search pairs are still collapsed correctly

Fixes #35694

Changed files

  • libs/partners/openai/langchain_openai/chat_models/_compat.py (modified, +5/-2)
  • libs/partners/openai/tests/unit_tests/chat_models/test_base.py (modified, +44/-0)

PR #35711: fix(langchain-openai): fix _consolidate_calls if/elif chain and UnboundLocalError on unknown tool names

Description (problem / solution / changelog)

Summary

Two bugs in _consolidate_calls() in libs/partners/openai/langchain_openai/chat_models/_compat.py:

Bug 1: Broken if/elif dispatch chain

The file_search branch used a bare if (line 314) instead of elif, making it an independent condition evaluated even after the web_search branch already matched:

# BEFORE (buggy)
if current.get("name") == "web_search":
    collapsed = ...
    collapsed["type"] = "web_search_call"

if current.get("name") == "file_search":   # ← bare 'if', not 'elif'!
    ...
elif current.get("name") == "code_interpreter":  # ← attaches to file_search, not web_search!
    ...

When a web_search call is processed, file_search condition is False (correct), but code_interpreter, remote_mcp, and mcp_list_tools branches are then unreachable from the web_search path because their elif is chained to file_search.

Fix: change the bare if for file_search to elif.

Bug 2: UnboundLocalError on unknown tool names

When server_tool_call.name doesn't match any known tool (web_search, file_search, code_interpreter, remote_mcp, mcp_list_tools), the else: pass branch never assigns collapsed, but yield collapsed on the next line raises UnboundLocalError: local variable 'collapsed' referenced before assignment.

Fix: in the else branch, yield current and nxt unchanged and continue to skip the yield collapsed line.

# AFTER (fixed)
else:
    # Unknown server_tool_call — emit both items unchanged
    yield current
    yield nxt
    continue

yield collapsed

Closes #35694

Changed files

  • docs/docs/integrations/chat/gigachat.ipynb (added, +141/-0)
  • libs/langchain/langchain/chat_models/base.py (modified, +10/-0)
  • libs/langchain/pyproject.toml (modified, +1/-0)
  • libs/partners/openai/langchain_openai/chat_models/_compat.py (modified, +408/-160)

PR #35741: fix(openai): correct if/elif chain and UnboundLocalError in _consolidate_calls

Description (problem / solution / changelog)

Summary

Fixes #35694

Two bugs in _consolidate_calls in libs/partners/openai/langchain_openai/chat_models/_compat.py:

  1. Bare if instead of elif: The file_search branch uses if instead of elif, breaking the if/elif chain. After matching web_search, execution falls through and also evaluates file_search, and the downstream elif blocks (code_interpreter, remote_mcp, mcp_list_tools) are only chained to file_search, not to the full conditional.

  2. UnboundLocalError on unknown tools: The else: pass branch doesn't assign collapsed, but yield collapsed executes unconditionally after it, raising UnboundLocalError for any unrecognized tool name.

Fix

  1. Changed if to elif on the file_search branch to restore the full if/elif chain.
  2. In the else branch, yield both current and nxt unchanged and continue (skip the yield collapsed) to handle unknown tool names gracefully.

Review notes

  • The if -> elif change on line 314 is the critical fix - without it, web_search calls always also evaluate the file_search condition, and the remaining branches are disconnected.
  • The else branch fix prevents a crash on any future or unknown server tool name.

Test plan

  • All 230 existing unit tests in tests/unit_tests/chat_models/ pass
  • ruff check and ruff format --check pass

This contribution was developed with AI assistance (Claude Code).

Changed files

  • libs/partners/openai/langchain_openai/chat_models/_compat.py (modified, +5/-2)

PR #36378: fix(openai): handle unknown tool names in _consolidate_calls

Description (problem / solution / changelog)

Summary

  • fix the broken if/elif dispatch in _consolidate_calls by making file_search part of the same chain
  • skip unknown tool names instead of falling through to yield collapsed
  • add regression tests for unknown tool names, web_search, and mixed known/unknown tool sequences

Testing

  • uv run --group test pytest tests/unit_tests/chat_models/test_compat_consolidate_calls.py -q

Fixes #35694

Changed files

  • libs/partners/openai/langchain_openai/chat_models/_compat.py (modified, +2/-2)
  • libs/partners/openai/tests/unit_tests/chat_models/test_compat_consolidate_calls.py (added, +44/-0)

Code Example

# in libs/partners/openai/langchain_openai/chat_models/_compat.py
# _consolidate_calls function
RAW_BUFFERClick to expand / collapse

Checked other resources

  • I searched existing issues
  • I searched the LangChain documentation
  • I searched the LangChain discussion forum

Example Code

# in libs/partners/openai/langchain_openai/chat_models/_compat.py
# _consolidate_calls function

Description

Two issues in _consolidate_calls:

  1. the file_search branch uses a bare if instead of elif, so it's evaluated even after web_search already matched — and the downstream elif/else chain attaches to file_search rather than the full dispatch.

  2. when a server_tool_call has a name that doesn't match any known tool (web_search, file_search, code_interpreter, etc.), the else branch does pass and falls through to yield collapsed where collapsed was never assigned, causing an UnboundLocalError.

System Info

langchain-openai latest

extent analysis

Fix Plan

Fix 1: Replace bare if with elif in _consolidate_calls

  • Step 1: Update the if statement in the file_search branch to use elif to ensure it's only evaluated when web_search hasn't matched.

  • Step 2: Update the elif/else chain to attach to the full dispatch instead of file_search.

Before

if web_search: # ... elif file_search: # ...

After

if web_search: # ... elif file_search or code_interpreter or server_tool_call: # ...


#### Fix 2: Handle unknown `server_tool_call` names

*   **Step 1:** Update the `else` branch to handle unknown `server_tool_call` names by raising a meaningful error or logging a warning.
*   **Step 2:** Update the `yield collapsed` statement to assign a default value to `collapsed` to avoid the `UnboundLocalError`.

    ```python
# Before
else:
    pass

# After
else:
    collapsed = "Unknown tool"
    # ...

Verification

  • Run the _consolidate_calls function with various inputs to ensure the fixes work as expected.
  • Verify that the file_search branch is only evaluated when web_search hasn't matched.
  • Verify that unknown server_tool_call names are handled correctly.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

langchain - ✅(Solved) Fix Bug: _consolidate_calls if/elif chain broken and UnboundLocalError on unknown tool names [4 pull requests, 6 comments, 5 participants]