langchain - 💡(How to fix) Fix [Bug] ChatDeepSeek drops reasoning_content in multi-turn conversations, causing 400 error with DeepSeek V4 reasoning models

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…

ChatDeepSeek fails with a 400 BadRequestError when using DeepSeek V4 reasoning models (deepseek-v4-pro, deepseek-v4-flash) in multi-turn conversations (e.g., with LangGraph agents). The error occurs because the reasoning_content from the assistant's response is not passed back to the API in subsequent requests, which is required by DeepSeek's thinking mode.

Error Message

openai.BadRequestError: Error code: 400 - {'error': {'message': 'The `reasoning_content` in the thinking mode must be passed back to the API.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

Root Cause

DeepSeek V4 models (with thinking mode enabled) return a reasoning_content field in the assistant message alongside the regular content. Per the DeepSeek API documentation, this reasoning_content must be included in the assistant message when making subsequent API calls in the same conversation.

The current langchain-deepseek code handles reasoning_content correctly on the response side — it stores it in AIMessage.additional_kwargs["reasoning_content"] (see chat_models.py:276-278).

However, the _get_request_payload method (which serializes messages back to the API format) does not write reasoning_content back into the assistant message dict. This means that in multi-turn conversations, the reasoning_content is silently dropped, causing the API to reject the request.

Code Example

openai.BadRequestError: Error code: 400 - {'error': {'message': 'The `reasoning_content` in the thinking mode must be passed back to the API.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

---

import os
from langchain_deepseek import ChatDeepSeek
from langchain_core.tools import tool
from langchain.agents import create_agent

os.environ["DEEPSEEK_API_KEY"] = "your-api-key"

llm = ChatDeepSeek(
    model="deepseek-v4-pro",
    temperature=0
)

@tool
def get_weather(city: str) -> str:
    """Get weather for a city."""
    return f"It's always sunny in {city}!"

agent = create_agent(
    model=llm,
    tools=[get_weather],
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)

---

def _get_request_payload(self, input_, *, stop=None, **kwargs):
    original_messages = self._convert_input(input_).to_messages()
    reasoning_contents = []
    for msg in original_messages:
        if isinstance(msg, AIMessage) and "reasoning_content" in msg.additional_kwargs:
            reasoning_contents.append(msg.additional_kwargs["reasoning_content"])
        else:
            reasoning_contents.append(None)

    payload = super()._get_request_payload(input_, stop=stop, **kwargs)

    for i, message in enumerate(payload["messages"]):
        if message["role"] == "assistant" and i < len(reasoning_contents) and reasoning_contents[i] is not None:
            message["reasoning_content"] = reasoning_contents[i]

    return payload
RAW_BUFFERClick to expand / collapse

Summary

ChatDeepSeek fails with a 400 BadRequestError when using DeepSeek V4 reasoning models (deepseek-v4-pro, deepseek-v4-flash) in multi-turn conversations (e.g., with LangGraph agents). The error occurs because the reasoning_content from the assistant's response is not passed back to the API in subsequent requests, which is required by DeepSeek's thinking mode.

Error Message

openai.BadRequestError: Error code: 400 - {'error': {'message': 'The `reasoning_content` in the thinking mode must be passed back to the API.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

Root Cause

DeepSeek V4 models (with thinking mode enabled) return a reasoning_content field in the assistant message alongside the regular content. Per the DeepSeek API documentation, this reasoning_content must be included in the assistant message when making subsequent API calls in the same conversation.

The current langchain-deepseek code handles reasoning_content correctly on the response side — it stores it in AIMessage.additional_kwargs["reasoning_content"] (see chat_models.py:276-278).

However, the _get_request_payload method (which serializes messages back to the API format) does not write reasoning_content back into the assistant message dict. This means that in multi-turn conversations, the reasoning_content is silently dropped, causing the API to reject the request.

Code Flow

  1. Round 1: User message → Model returns {content: "...", reasoning_content: "thinking process..."}
  2. ChatDeepSeek._create_chat_result() correctly stores reasoning_content in AIMessage.additional_kwargs
  3. Round 2: User message + Assistant message → _get_request_payload() serializes the assistant message without reasoning_content → API returns 400 error

Reproduction

import os
from langchain_deepseek import ChatDeepSeek
from langchain_core.tools import tool
from langchain.agents import create_agent

os.environ["DEEPSEEK_API_KEY"] = "your-api-key"

llm = ChatDeepSeek(
    model="deepseek-v4-pro",
    temperature=0
)

@tool
def get_weather(city: str) -> str:
    """Get weather for a city."""
    return f"It's always sunny in {city}!"

agent = create_agent(
    model=llm,
    tools=[get_weather],
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)

This fails because the agent makes multiple LLM calls (model → tool call → model), and the second call drops reasoning_content.

Suggested Fix

The _get_request_payload method in ChatDeepSeek should be updated to include reasoning_content from AIMessage.additional_kwargs when serializing assistant messages:

def _get_request_payload(self, input_, *, stop=None, **kwargs):
    original_messages = self._convert_input(input_).to_messages()
    reasoning_contents = []
    for msg in original_messages:
        if isinstance(msg, AIMessage) and "reasoning_content" in msg.additional_kwargs:
            reasoning_contents.append(msg.additional_kwargs["reasoning_content"])
        else:
            reasoning_contents.append(None)

    payload = super()._get_request_payload(input_, stop=stop, **kwargs)

    for i, message in enumerate(payload["messages"]):
        if message["role"] == "assistant" and i < len(reasoning_contents) and reasoning_contents[i] is not None:
            message["reasoning_content"] = reasoning_contents[i]

    return payload

Environment

  • langchain-deepseek: latest (installed via pip)
  • langchain: installed via pip
  • langgraph: 1.2.2
  • Python: 3.11
  • OS: Windows 10
  • Model: deepseek-v4-pro (also affects deepseek-v4-flash and any DeepSeek model with thinking mode)

Additional Context

  • This issue affects all DeepSeek reasoning/thinking models, not just V4 Pro.
  • The legacy deepseek-reasoner model has the same requirement but was less commonly used with agents.
  • With the release of DeepSeek V4 (which has thinking mode enabled by default), this issue becomes much more impactful since V4 models are now widely used with LangGraph agents.
  • Per the DeepSeek V4 guide: "the multi-turn reasoning_content 400 error is real and breaks every popular client on first contact."

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 - 💡(How to fix) Fix [Bug] ChatDeepSeek drops reasoning_content in multi-turn conversations, causing 400 error with DeepSeek V4 reasoning models