Repository navigation
Description
Description
When using DeepSeek V4 Pro/Flash (deepseek-v4-pro / deepseek-v4-flash) via the official DeepSeek API or OpenRouter, multi-turn conversations with tool calls fail with a 400 error starting from the second turn. The first turn always works; subsequent turns fail.
Error message:
400 The `reasoning_content` in the thinking mode must be passed back to the API.
Full error payload:
{
"error": {
"message": "The `reasoning_content` in the thinking mode must be passed back to the API.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_request_error"
}
}When it happens:
- Interrupted and resumed sessions
- Subagent / Task tool invocations
- Any scenario where the conversation history includes an assistant message with
tool_callsfrom a prior turn
Root Cause
DeepSeek V4 enables thinking mode by default. Each assistant response includes a reasoning_content field alongside content and optionally tool_calls. Per the DeepSeek Thinking Mode documentation:
Between two
usermessages, if the model performed a tool call, the intermediate assistant'sreasoning_contentmust participate in the context concatenation and must be passed back to the API in all subsequent user interaction turns.
OpenCode's provider layer currently strips reasoning_content when constructing message history for subsequent API requests. This means:
- Turn 1: User → API → Assistant responds with
reasoning_content+tool_calls→ ✅ Works - Turn 2: OpenCode sends history to API but assistant message missing
reasoning_content→ ❌ 400 error - All subsequent turns fail the same way
This is the same bug that has affected multiple AI coding tools with the V4 release (Kilo Code, OpenClaude, Hermes Agent, Roo Code, etc.) — nearly all of them have published fixes or PRs within the last 24 hours.
Steps to reproduce
- Configure OpenCode with DeepSeek provider / OpenRouter using
deepseek-v4-proordeepseek-v4-flash - Start a conversation and send a prompt that triggers a tool call (e.g., "search for X" or "read file Y")
- The first turn completes successfully (tool executes, result returned)
- Send a follow-up message in the same session → 400 error
Consistently reproducible. Simple chat without tools works fine because no reasoning_content requirement is triggered.
Expected behavior
OpenCode should preserve reasoning_content on assistant messages in the request history when using DeepSeek V4 models. The field should be echoed back verbatim on every assistant message that originally contained it (or empty string for turns without reasoning).
Suggested fix direction
Based on similar fixes in other projects (e.g., gitlawb/openclaude#878):
- Detect when the model is a DeepSeek V4 series (by model name prefix or base URL containing
deepseek.com) - Capture
reasoning_contentfrom each streaming/non-streaming assistant response - Re-attach it to the corresponding assistant message when building the request history for subsequent API calls
- This should be gated to DeepSeek V4 only to avoid affecting OpenAI / Anthropic / other providers
Additional context
- This affects both the official DeepSeek API (
api.deepseek.com) and OpenRouter using DeepSeek V4 models - Legacy DeepSeek models (
deepseek-chat,deepseek-reasoner) are NOT affected — they use the opposite rule (do NOT pass back reasoning) - Simple chat (no tools) works because no
reasoning_contentround-trip requirement is triggered
Plugins
No response
OpenCode version
1.14.24
Steps to reproduce
See "Steps to reproduce" section above.
Screenshot and/or share link
No response
Operating System
macOS
Terminal
kitty / VS Code terminal
Activity
ihoooohi commentedon Apr 24, 2026
Related: #24188 (same issue, filed separately for clarity — the original had unrelated Flutter output mixed in the body).
github-actions commentedon Apr 24, 2026
This issue might be a duplicate of existing issues. Please check:
reasoning_contentin the thinking mode must be passed back to the API. #24188: Same error —reasoning_contentmust be passed back to the API (open)Please review these issues to see if your report is already covered before this one is triaged.
kipropbrian commentedon Apr 24, 2026
It seems the fix did not fix this, it fails on the second turn.
totalard commentedon Apr 24, 2026
it still showing up,
just do a plan with deepseek flash, and then change agent to build, and type proceed, you see the error
tariky commentedon Apr 24, 2026
After tool calls it just fails with this error
[DeepSeek] Thereasoning_contentin the thinking mode must be passed back to the API.I tried to make extension to fix this issue but no luck.
ihoooohi commentedon Apr 24, 2026
I have opened a PR to fix this: #24200
The fix addresses the remaining code paths where empty
reasoning_contentwas being dropped — specifically in the non-streaming parser, streaming parser, and outbound converter. This complements PR #24146 which fixed thetransform.tspath.Feel free to review: #24200
rekram1-node commentedon Apr 24, 2026
That PR does not work
rekram1-node commentedon Apr 24, 2026
Can someone send me a session it errored in? I'm unable to reproduce maybe if you are using openrouter there is an openrouter specific issue but if not then i cannot repro.
bestouff commentedon Apr 24, 2026
How do I do that ?
rekram1-node commentedon Apr 24, 2026
opencode export > session.jsonRun this exact command in ur terminal and send me the json file
bestouff commentedon Apr 24, 2026
Sent by mail to [email protected] - hope it's ok
kipropbrian commentedon Apr 24, 2026
I just dumped the session.md and I cannot see the error '[DeepSeek] The
reasoning_contentin the thinking mode must be passed back to the API.' Yet its in openecode itself. So maybe the export command, does not capture it?rekram1-node commentedon Apr 24, 2026
@kipropbrian please run exactly what I said, if there is any .md file you misread my instructions, run this EXACT command in your terminal:
opencode export > session.json
3 remaining items
Jay4242 commentedon Apr 24, 2026
I'm still getting the error after
opencode models --refreshon v1.14.24. Another session.jsonArecsu commentedon Apr 25, 2026
Related:
earendil-works/pi#3668
openclaw/openclaw#71160
Commit in
pi-monothatfixes itedit: seems it isn't enough, check my next commentearendil-works/pi@9b103e5
Arecsu commentedon Apr 25, 2026
Alright, I managed to fix it. There's two parts of the problem, one is Opencode, the other is OpenRouter. The OpenRouter is somewhat funny honestly
At opencode,
provider.ts,fromModelsDevModeldefaultsinterleavedtofalsefor reasoning modelsWhen building models from the models.dev snapshot,
interleavedis hardcoded tofalsewhen no explicit value is set. This meansexistingModel?.capabilities.interleavedresolves tofalse(notundefined), so the??fallback at line 1180 never fires. Fix:Then the OpenRouter one, which strips
reasoning: ""before forwarding to DeepSeekEven after adding
reasoning: ""to every assistant message in the request body, DeepSeek still rejects the request because OpenRouter silently drops empty string values before forwarding.I managed to trick it into not dropping the string by placing a unicode zero-width space. Cool, isn't?
Inside
options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {atprovider.tsPR #24218 only addresses part of issue 1, and only for user-configured models. It misses the models.dev path and the OpenRouter empty-string stripping entirely.
And I don't think this hack should make its way into Opencode. I'm just sharing it for any of the bots spamming untested PRs so they stop wasting their tokens on this, and for everybody else if they want to fork it and feel desperate, well here's your ticket to a working build :)
If anyone could point this out to OpenRouter (might they been aware of this already? not sure), would be nice.
Found @alexanderatallah from OpenRouter. Check this out!
rekram1-node commentedon Apr 25, 2026
Seems to be a bug in openrouter sdk rather than our code
Lauri-Nomme commentedon Apr 25, 2026
@rekram1-node you could still consider doing a compensating workaround in opencode for the time being?
Svtter commentedon Apr 25, 2026
kipropbrian commentedon Apr 25, 2026
Using Claude Code, open router and deepseek-v4-flash seems to work so it may not be an openrouter issue.I have to eat my words, same issue still pops up. It seems to be an openrouter issue.
rekram1-node commentedon Apr 26, 2026
Sorry for delay yall, was traveling but have a PR that I validated should behave as expected w/ openrouter now, contains a small code change and a bump to openrouter sdk package that we use, I talked w/ openrouter team and they made this patch specifically due to us raising the issue.
rekram1-node commentedon Apr 26, 2026
Will release soon
kipropbrian commentedon Apr 26, 2026
I upgraded to 1.14.25 via npm as brew is stuck at an earlier version. The issues is still there. https://gist.github.com/kipropbrian/9cda3235f80bbb237cbd5ffb86e8207e here is my session export.
rekram1-node commentedon Apr 27, 2026
@kipropbrian you are on incorrect version, fix was in 1.14.26
kipropbrian commentedon Apr 27, 2026
This issue is still there unfortunately @rekram1-node "version": "1.14.28", https://gist.github.com/kipropbrian/258d11457a5f7fea3ad02b97e22788b0
rekram1-node commentedon Apr 27, 2026
@kipropbrian I think there is a second bug in openrouter sdk, I will continue discussion here:
#24569 (comment)
feat(opencode): research-backed agent tuning (#225)