Skip to content

fix: preserve reasoning_content in multi-turn tool-call sessions (MiMo compatibility)#259

Open
Noogear wants to merge 3 commits into
JohnnyZ93:mainfrom
Noogear:main
Open

fix: preserve reasoning_content in multi-turn tool-call sessions (MiMo compatibility)#259
Noogear wants to merge 3 commits into
JohnnyZ93:mainfrom
Noogear:main

Conversation

@Noogear
Copy link
Copy Markdown

@Noogear Noogear commented May 18, 2026

Problem

When using models like Xiaomi MiMo that require reasoning_content (or thinking blocks) to be preserved in historical assistant messages containing tool_calls, the API returns 400 error on subsequent requests in multi-turn agent sessions.

This happens because VS Code's LanguageModelThinkingPart is not guaranteed to be preserved in conversation history between requests, causing reasoning content to be lost when reconstructing messages.

See: https://platform.xiaomimimo.com/docs/zh-CN/usage-guide/passing-back-reasoning_content

Solution

Implemented a 3-level reasoning content caching mechanism, gated by the existing include_reasoning_in_request config option (opt-in only):

  1. Map cache (_reasoningContentCache): keyed by sorted tool-call IDs, persists across requests within a session
  2. Last reasoning fallback (_lastReasoningContent): stores the most recent reasoning content as a cross-restart fallback
  3. Placeholder fallback: "Next step." when no cached content is available

Files changed

File Change
src/commonApi.ts Added reasoning content accumulation (_accumulatedReasoningContent) and tool-call ID tracking (_emittedToolCallIds) during streaming
src/openai/openaiApi.ts convertMessages() now accepts optional reasoning cache; restores reasoning_content for assistant messages with tool_calls
src/anthropic/anthropicApi.ts convertMessages() now accepts optional reasoning cache; restores thinking content blocks for assistant messages with tool_use (was completely missing before)
src/provider.ts Added _reasoningContentCache Map and _lastReasoningContent string; passes cache to both OpenAI and Anthropic paths; stores reasoning content after streaming completes

Behavior

  • Only activates when include_reasoning_in_request: true is set in model config
  • No model ID detection or auto-detection — purely opt-in
  • Works for both OpenAI Chat Completions and Anthropic protocol modes

Testing

Verified with Xiaomi MiMo (mimo-v2.5-pro) using Anthropic protocol mode.
Multi-turn agent sessions with tool calls no longer produce 400 errors.

Config example:

{
  "id": "mimo-v2.5-pro",
  "apiMode": "anthropic",
  "baseUrl": "https://token-plan-sgp.xiaomimimo.com/anthropic",
  "include_reasoning_in_request": true
}

Noogear added 3 commits May 19, 2026 09:21
…sions

MiMo API returns 400 when historical assistant messages with tool_calls
are missing reasoning_content. VS Code's LanguageModelThinkingPart may
not be preserved in conversation history between requests.

Changes:
- commonApi: accumulate raw reasoning_content during streaming, track
  emitted tool call IDs, expose getters
- openaiApi: convertMessages accepts optional reasoningContentCache and
  lastReasoningContent; replays cached reasoning for assistant messages
  with tool_calls when VS Code history lacks thinking parts
- provider: manage reasoning content cache (Map) and lastReasoningContent
  (string) across requests; pass to convertMessages and store after stream

Fallback chain (tool_calls messages only):
  joinedThinking -> cache by tool-call IDs -> lastReasoningContent -> placeholder

Only activates when include_reasoning_in_request: true is set.
The previous fix only covered OpenAI mode. The Anthropic path had the
identical problem: when VS Code history doesn't preserve
LanguageModelThinkingPart, thinking content is lost and falls back to
placeholder 'Next step.', causing MiMo 400 errors on Anthropic endpoint.

Changes:
- anthropicApi: convertMessages now accepts reasoningContentCache and
  lastReasoningContent, with same fallback chain as openaiApi
- provider: pass cache to Anthropic convertMessages, store reasoning
  content after streaming (mirrors OpenAI path)
@RayWangQvQ
Copy link
Copy Markdown

Same issue:

Sorry, your request failed. Please try again.

Copilot Request id: a0cd499a-e5b7-46b0-947d-0c4f9eead4ec

Reason: OAI Compatible API error: [400] Bad Request {"error":{"code":"400","message":"Param Incorrect","param":"The reasoning_content in the thinking mode must be passed back to the API.","type":""}} URL: [https://token-plan-cn.xiaomimimo.com/v1/chat/completions](vscode-file://vscode-app/d:/Program%20Files/Microsoft%20VS%20Code/0958016b2a/resources/app/out/vs/code/electron-browser/workbench/workbench.html): Error: OAI Compatible API error: [400] Bad Request {"error":{"code":"400","message":"Param Incorrect","param":"The reasoning_content in the thinking mode must be passed back to the API.","type":""}} URL: [https://token-plan-cn.xiaomimimo.com/v1/chat/completions](vscode-file://vscode-app/d:/Program%20Files/Microsoft%20VS%20Code/0958016b2a/resources/app/out/vs/code/electron-browser/workbench/workbench.html) at c:\Users\ray.vscode\extensions\johnny-zhao.oai-compatible-copilot-0.4.2\out\provider.js:428:31 at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async executeWithRetry (c:\Users\ray.vscode\extensions\johnny-zhao.oai-compatible-copilot-0.4.2\out\utils.js:302:20) at async HuggingFaceChatModelProvider.provideLanguageModelChatResponse (c:\Users\ray.vscode\extensions\johnny-zhao.oai-compatible-copilot-0.4.2\out\provider.js:419:34)

Pls merge it.

@JohnnyZ93
Copy link
Copy Markdown
Owner

Did you set "include_reasoning_in_request": true?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants