langchain - ✅(Solved) Fix openai model: tool_choice="required" not compatible with thinking models (qwen3.5-plus) [1 pull requests, 3 comments, 3 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#36120Fetched 2026-04-08 01:03:30
View on GitHub
Comments
3
Participants
3
Timeline
8
Reactions
0
Timeline (top)
labeled ×3commented ×2cross-referenced ×1issue_type_added ×1

Error Message

System Info

  • langchain-openai version: 0.1.20
  • langchain version: 1.2.x
  • model: qwen3.5-plus (Tongyi)
  • base_url: Dashscope compatible OpenAI protocol

Issue Description

When using thinking models (like qwen3.5-plus, deepseek-r1) with ChatOpenAI / init_chat_model, the LangChain agent automatically sets tool_choice="required", which causes the API to return a 400 error:

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

PR fix notes

PR #36135: fix(openai): downgrade tool_choice='required' to 'auto' for reasoning models

Description (problem / solution / changelog)

Fixes #36120

Problem

When using reasoning/thinking models (o1, o3, gpt-5, qwen3.5-plus, etc.) with LangChain agents, the agent factory auto-sets tool_choice='any' which gets normalized to tool_choice='required' in ChatOpenAI.bind_tools(). Reasoning models reject tool_choice='required' with HTTP 400 errors.

Changes

In ChatOpenAI.bind_tools(), after the tool_choice normalization block converts 'any''required' and True'required', added a check: if the resulting tool_choice is 'required' AND the model is a reasoning model, downgrade to 'auto' with a warning.

A reasoning model is detected by:

  1. self.profile has reasoning_output=True, OR
  2. self.reasoning_effort is set, OR
  3. self.reasoning is set

Test plan

  • Added unit tests verifying reasoning models get 'auto' instead of 'required'
  • Added regression tests verifying non-reasoning models still get 'required'
  • Added tests verifying explicit tool_choice='auto'/'none' are unaffected
  • All 324 existing tests pass

Disclaimer

This PR was developed with AI agent assistance.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +21/-0)
  • libs/partners/openai/tests/unit_tests/chat_models/test_base.py (modified, +86/-0)

Code Example

### System Info
- langchain-openai version: 0.1.20
- langchain version: 1.2.x
- model: qwen3.5-plus (Tongyi)
- base_url: Dashscope compatible OpenAI protocol

### Issue Description
When using **thinking models** (like qwen3.5-plus, deepseek-r1) with `ChatOpenAI / init_chat_model`, the LangChain agent automatically sets `tool_choice="required"`, which causes the API to return a 400 error:

---

Thinking models **do NOT support `tool_choice="required"`**, only "auto" or "none".

But in `langchain/agents/factory.py`, `_execute_model_sync` forces `tool_choice="required"`, which breaks all thinking models.

### Error Log
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

System Info

  • langchain-openai version: 0.1.20
  • langchain version: 1.2.x
  • model: qwen3.5-plus (Tongyi)
  • base_url: Dashscope compatible OpenAI protocol

Issue Description

When using thinking models (like qwen3.5-plus, deepseek-r1) with ChatOpenAI / init_chat_model, the LangChain agent automatically sets tool_choice="required", which causes the API to return a 400 error:

Reproduction Steps / Example Code (Python)

### System Info
- langchain-openai version: 0.1.20
- langchain version: 1.2.x
- model: qwen3.5-plus (Tongyi)
- base_url: Dashscope compatible OpenAI protocol

### Issue Description
When using **thinking models** (like qwen3.5-plus, deepseek-r1) with `ChatOpenAI / init_chat_model`, the LangChain agent automatically sets `tool_choice="required"`, which causes the API to return a 400 error:

Error Message and Stack Trace (if applicable)

Thinking models **do NOT support `tool_choice="required"`**, only "auto" or "none".

But in `langchain/agents/factory.py`, `_execute_model_sync` forces `tool_choice="required"`, which breaks all thinking models.

### Error Log

Description

Suggestion

Add a check for thinking models, or allow users to force override tool_choice without modifying source code.

Or make tool_choice="auto" default for agent model calls.

System Info

Suggestion

Add a check for thinking models, or allow users to force override tool_choice without modifying source code.

Or make tool_choice="auto" default for agent model calls.

extent analysis

Fix Plan

To resolve the issue, we need to modify the langchain library to handle thinking models correctly. Here are the steps:

  • Modify the _execute_model_sync function in langchain/agents/factory.py to allow for a default tool_choice of "auto" or to override the tool_choice parameter.
  • Add a check for thinking models and set tool_choice to "auto" by default for these models.

Example Code

# In langchain/agents/factory.py

def _execute_model_sync(self, model, prompt, tool_choice="auto"):
    # ... existing code ...

    if model.startswith("thinking-") or model in ["qwen3.5-plus", "deepseek-r1"]:
        tool_choice = "auto"

    # ... existing code ...

Alternatively, you can add an optional tool_choice parameter to the init_chat_model function:

# In langchain/agents/factory.py

def init_chat_model(self, model, tool_choice="auto"):
    # ... existing code ...

    self._execute_model_sync(model, prompt, tool_choice=tool_choice)

Then, when initializing the chat model, you can specify the tool_choice parameter:

# Example usage
agent = ChatOpenAI()
agent.init_chat_model("qwen3.5-plus", tool_choice="auto")

Verification

To verify that the fix worked, you can test the init_chat_model function with a thinking model and check that the API returns a successful response:

# Example test
agent = ChatOpenAI()
agent.init_chat_model("qwen3.5-plus", tool_choice="auto")
response = agent.get_response("Hello, world!")
print(response)  # Should print a successful response

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