Skip to content

[DeepSeek V4] reasoning_content not round-tripped — 400 error on multi-turn tool calls #24190

Closed

Description

@ihoooohi

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_calls from 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 user messages, if the model performed a tool call, the intermediate assistant's reasoning_content must 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:

  1. Turn 1: User → API → Assistant responds with reasoning_content + tool_calls → ✅ Works
  2. Turn 2: OpenCode sends history to API but assistant message missing reasoning_content → ❌ 400 error
  3. 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

  1. Configure OpenCode with DeepSeek provider / OpenRouter using deepseek-v4-pro or deepseek-v4-flash
  2. Start a conversation and send a prompt that triggers a tool call (e.g., "search for X" or "read file Y")
  3. The first turn completes successfully (tool executes, result returned)
  4. 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):

  1. Detect when the model is a DeepSeek V4 series (by model name prefix or base URL containing deepseek.com)
  2. Capture reasoning_content from each streaming/non-streaming assistant response
  3. Re-attach it to the corresponding assistant message when building the request history for subsequent API calls
  4. 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_content round-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

  1. ihoooohi commented on Apr 24, 2026

    @ihoooohi
    Author

    Related: #24188 (same issue, filed separately for clarity — the original had unrelated Flutter output mixed in the body).

  2. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 24, 2026
  3. github-actions commented on Apr 24, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Please review these issues to see if your report is already covered before this one is triaged.

  4. kipropbrian commented on Apr 24, 2026

    @kipropbrian

    It seems the fix did not fix this, it fails on the second turn.

  5. totalard commented on Apr 24, 2026

    @totalard

    it still showing up,

    just do a plan with deepseek flash, and then change agent to build, and type proceed, you see the error

  6. tariky commented on Apr 24, 2026

    @tariky

    After tool calls it just fails with this error [DeepSeek] The reasoning_content in the thinking mode must be passed back to the API.

    I tried to make extension to fix this issue but no luck.

  7. ihoooohi commented on Apr 24, 2026

    @ihoooohi
    Author

    I have opened a PR to fix this: #24200

    The fix addresses the remaining code paths where empty reasoning_content was being dropped — specifically in the non-streaming parser, streaming parser, and outbound converter. This complements PR #24146 which fixed the transform.ts path.

    Feel free to review: #24200

  8. rekram1-node commented on Apr 24, 2026

    @rekram1-node
    Collaborator

    That PR does not work

  9. rekram1-node commented on Apr 24, 2026

    @rekram1-node
    Collaborator

    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.

  10. bestouff commented on Apr 24, 2026

    @bestouff

    How do I do that ?

  11. rekram1-node commented on Apr 24, 2026

    @rekram1-node
    Collaborator

    opencode export > session.json

    Run this exact command in ur terminal and send me the json file

  12. bestouff commented on Apr 24, 2026

    @bestouff

    Sent by mail to [email protected] - hope it's ok

  13. kipropbrian commented on Apr 24, 2026

    @kipropbrian

    I just dumped the session.md and I cannot see the error '[DeepSeek] The reasoning_content in the thinking mode must be passed back to the API.' Yet its in openecode itself. So maybe the export command, does not capture it?

  14. rekram1-node commented on Apr 24, 2026

    @rekram1-node
    Collaborator

    @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

  15. 3 remaining items

  16. Jay4242 commented on Apr 24, 2026

    @Jay4242

    I'm still getting the error after opencode models --refresh on v1.14.24. Another session.json

  17. Arecsu commented on Apr 25, 2026

    @Arecsu

    Related:

    earendil-works/pi#3668
    openclaw/openclaw#71160

    Commit in pi-mono that fixes it edit: seems it isn't enough, check my next comment

    earendil-works/pi@9b103e5

  18. Arecsu commented on Apr 25, 2026

    @Arecsu

    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, fromModelsDevModel defaults interleaved to false for reasoning models

    When building models from the models.dev snapshot, interleaved is hardcoded to false when no explicit value is set. This means existingModel?.capabilities.interleaved resolves to false (not undefined), so the ?? fallback at line 1180 never fires. Fix:

    interleaved: model.interleaved ?? (model.reasoning ? { field: "reasoning_content" } : false),

    Then the OpenRouter one, which strips reasoning: "" before forwarding to DeepSeek

    Even 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) => { at provider.ts

    if (model.api.npm === "@openrouter/ai-sdk-provider" && model.api.id.includes("deepseek") && opts.body && opts.method === "POST") {
      const parsed = JSON.parse(opts.body as string)
      let modified = false
      for (const msg of parsed.messages ?? []) {
        if (msg.role === "assistant" && !msg.reasoning) {
          msg.reasoning = "​" // ZERO-WIDTH SPACE HACK INSIDE THE QUOTES
          modified = true
        }
      }
      if (modified) opts.body = JSON.stringify(parsed)
    }

    PR #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!

  19. rekram1-node commented on Apr 25, 2026

    @rekram1-node
    Collaborator

    Seems to be a bug in openrouter sdk rather than our code

  20. Lauri-Nomme commented on Apr 25, 2026

    @Lauri-Nomme

    @rekram1-node you could still consider doing a compensating workaround in opencode for the time being?

  21. Svtter commented on Apr 25, 2026

    @Svtter
  22. kipropbrian commented on Apr 25, 2026

    @kipropbrian

    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.

    Image
  23. rekram1-node commented on Apr 26, 2026

    @rekram1-node
    Collaborator

    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.

  24. rekram1-node commented on Apr 26, 2026

    @rekram1-node
    Collaborator

    Will release soon

  25. kipropbrian commented on Apr 26, 2026

    @kipropbrian

    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.

  26. rekram1-node commented on Apr 27, 2026

    @rekram1-node
    Collaborator

    @kipropbrian you are on incorrect version, fix was in 1.14.26

  27. rekram1-node commented on Apr 27, 2026

    @rekram1-node
    Collaborator

    @kipropbrian I think there is a second bug in openrouter sdk, I will continue discussion here:
    #24569 (comment)

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

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions