magpie icon indicating copy to clipboard operation
magpie copied to clipboard

[bug] Responses → Chat for Mistral: `deepseek-*` model gets `reasoning_content` echoed back → 422 extra_forbidden (Codex breaks after first tool call)

Open MystryMocha opened this issue 1 month ago • 7 comments

Problem

Follow-up to #494 (fixed in 0.1.616 for the Chat passthrough). The Chat Completions route still works, but the /v1/responses → /v1/chat/completions route (what Codex uses) sends an earlier assistant turn's reasoning to Mistral as reasoning_content when the model name is DeepSeek-like. Mistral rejects it:

Mistral10977752: 422 Unprocessable Entity: {"detail":[{"type":"extra_forbidden","loc":["body","messages",1,"assistant","reasoning_content"],"msg":"Extra inputs are not permitted","input":"I should read note.txt with the tool."}]}

In Codex the first request of a turn succeeds; every follow-up after a tool call fails with 422 (repeated, so the "retry without the rejected field and remember it" fallback from #494 does not kick in on this route either).

Environment

Item Value
Magpie 0.1.2710 (Windows app)
Client Codex Desktop 0.166.0-alpha.5.1 (effort: max)
Provider mistral preset, https://api.mistral.ai/v1
Model deepseek-v4-1-flash-1017

Reproduction

curl http://127.0.0.1:3425/v1/responses \
  -H 'Authorization: Bearer magpie' -H 'Content-Type: application/json' \
  -d '{"model":"<mistral-provider>/deepseek-v4-1-flash-1017","stream":false,"max_output_tokens":40,
       "tools":[{"type":"function","name":"read_file","parameters":{"type":"object","properties":{"path":{"type":"string"}}}}],
       "input":[
         {"type":"message","role":"user","content":[{"type":"input_text","text":"Read note.txt and tell me the word."}]},
         {"type":"reasoning","summary":[{"type":"summary_text","text":"I should read note.txt with the tool."}]},
         {"type":"function_call","call_id":"call_1","name":"read_file","arguments":"{\"path\":\"note.txt\"}"},
         {"type":"function_call_output","call_id":"call_1","output":"pelican"}]}'
Request Result
/v1/responses, deepseek-v4-1-flash-1017 422 extra_forbidden reasoning_content
/v1/responses, zai-glm-5-3 (same provider) 200
/v1/responses, mistral-medium-latest (same provider) 200
/v1/chat/completions with reasoning_content on an assistant turn, deepseek-v4-1-flash-1017 200

So it looks like the DeepSeek "reasoning must be passed back" handling (keyed on the model name) re-adds reasoning_content after the Mistral-specific conversion to a thinking content part, on the Responses route only. Possibly the same name-keyed injection as #3165 / withReasoningOnToolTurns in #2607, but here the upstream is Mistral, which rejects the field outright.

Expected

For a Mistral upstream (preset or api.mistral.ai host), never send reasoning_content / reasoning / reasoning_details as message fields regardless of the model name, same as the Chat route since 0.1.616.

MystryMocha avatar Nov 02 '26 09:11 MystryMocha

Conclusion

Reproduced and fixed the Mistral translation case on Windows/amd64, Go 1.27.0, at ed978e56 on 2026-11-02. For a Chat request built for Mistral, the final translated body now goes through the existing mistralThinking conversion before it is sent.

Evidence

Check Result
Baseline Responses-to-Chat request 422 extra_forbidden at messages[].reasoning_content
After conversion 200
Same body after disabling the new branch 422 again
Mistral thinking-part test pass
Translated DeepSeek comparison pass

The upstream never received reasoning_content, reasoning, or reasoning_details as message fields in the passing case; the earlier reasoning remains as Mistral's typed thinking content part. gofmt, Windows/Linux vet, and Windows/Linux/macOS builds passed.

Boundary

The branch uses the same Mistral predicate as the direct Chat path: preset mistral or host api.mistral.ai. Other Chat providers keep their existing reasoning replay behavior. The direct Chat path is unchanged and does not run the conversion twice.

Gap

Not tried: a real Mistral API request. The full gateway suite is not green in this environment, but the failing Windows/WSL and JSON cases reproduce at ed978e56 without this change.

Action

A minimal local diff is ready. I can put the 12-line implementation patch here if useful.

Edward4226 avatar Nov 02 '26 13:11 Edward4226

Root cause. buildChat replays an assistant turn's thinking in reasoning_content whenever chatReplaysReasoning says the upstream wants it back — and that predicate keys on the model name ("deepseek" anywhere in it), wherever the model is actually served. So a deepseek-* model on Mistral had reasoning_content injected on every tool turn of a translated request. The conversion that saves this (mistralThinking, from the #494 fix) ran only on the Chat passthrough route, never on a translated build — which is exactly your A/B: /v1/chat/completions with reasoning_content on the same provider answered 200 (the relay converts the field), while /v1/responses 422ed on every follow-up after the first tool call (the translation injects the field and nothing converts it). The "retry without the rejected field" fallback from #494 can't kick in because the field is magpie's own injection on this route, not a client field it could remember.

Fix (branch fix/4906-mistral-translated-reasoning):

  • forwardTranslated now runs the same mistralThinking conversion on the body built for a Mistral upstream (preset mistral or api.mistral.ai — the identical gate the relayed route uses), beside the other per-protocol post-build mends: the reasoning fields come out of every message and the thinking goes in as the typed thinking part Mistral itself returns it in, ahead of the turn's content.
  • Everything else unchanged: other upstreams keep the #1462 replay behavior (a DeepSeek at DeepSeek still gets reasoning_content), and the relayed Chat route already had the conversion.

Verification.

  • TestTranslatedReasoningAsThinkingForMistral: your request verbatim (reasoning item → function_call → function_call_output) on mistral/deepseek-v4-1-flash-1017. On current main the upstream answers your exact 422 — loc ["body","messages",1,"assistant","reasoning_content"], "Extra inputs are not permitted", input "I should read note.txt with the tool." (red log recorded); with the fix no message carries reasoning_content, and the reasoning item's summary reaches Mistral as a thinking part.
  • Scope guards: TestTranslatedToolTurnWithoutReasoning (DeepSeek elsewhere keeps the replay), TestChatToolTurnWithoutReasoningNotForMistral and TestMistralReasoningAsThinking (relay unchanged) all pass; gofmt/go vet/go build clean. Doc line added in docs/subsystems/gateway-routing.md.

Branch: https://github.com/skygunner/magpie/tree/fix/4906-mistral-translated-reasoning (head 29b0ea308)

Boundary: not verified against the real Mistral API — no key here. The refusal and the accepted thinking-part shape are the ones already encoded from #494 (mistralThinking and its tests); the red log reproduces your 422 byte for byte in field, location and input. zai-glm-5-3 and mistral-medium-latest never tripped this because their names don't say DeepSeek, so nothing was injected for them.

This is a fork branch, ready for review, not a claim on the issue's ownership.

skygunner avatar Nov 02 '26 21:11 skygunner

Conclusion

I rebuilt and validated the Mistral translation fix on current origin/main (173b7e2742530dc6c9b7cd14f63cbf8eafc79f9c) and published a complete cherry-pickable patch. A translated Chat body for Mistral now passes through the existing thinking-part conversion before it is sent.

Evidence

  • Branch: https://github.com/Edward4226/magpie/tree/codex/issue-4906-mistral
  • Commit: https://github.com/Edward4226/magpie/commit/0b8aee26fefe1e1f3f14dc252237d922da071291
  • Patch: https://github.com/Edward4226/magpie/commit/0b8aee26fefe1e1f3f14dc252237d922da071291.patch
  • Compare: https://github.com/Edward4226/magpie/compare/173b7e2742530dc6c9b7cd14f63cbf8eafc79f9c...codex/issue-4906-mistral
  • go test -tags nogui ./internal/gateway -run '^TestTranslatedMistralReasoningAsThinking$' -count=1: pass
  • gofmt, git diff --check, Windows/Linux go vet, Windows/Linux/macOS top-level builds: pass

The test includes the DeepSeek-elsewhere control, which still receives reasoning_content; Mistral receives a typed thinking part instead. This is an independent implementation, interchangeable with skygunner's branch above, not a claim that only this patch works.

Boundary

The Mistral gate matches the existing direct-Chat gate: preset mistral or host api.mistral.ai. Other providers and the direct Chat path are unchanged.

Gap

Not tried: a real Mistral API request; the full gateway suite.

Action

Cherry-pick 0b8aee26f, or run:

git fetch https://github.com/Edward4226/magpie.git codex/issue-4906-mistral
git cherry-pick 0b8aee26fefe1e1f3f14dc252237d922da071291

Do not apply both fork branches and skygunner's branch; their behavior is the same. The issue remains open.

Edward4226 avatar Nov 02 '26 22:11 Edward4226

Thanks @skygunner for the first complete fix, and @Edward4226 for the independent reproduction and patch.

I tested skygunner's 29b0ea308 on current main (a92cc3f3), Windows/amd64, Go 1.27.3. It cherry-picks cleanly. Verified end to end with the real Mistral API and real Codex.

Real Codex session. I built main + the fix and ran it as the gateway on my usual port. In Codex Desktop with mistral/deepseek-v4-1-flash-1017 (effort max), I sent the same prompt that failed on 0.1.2782 ("draw a pelican riding a bicycle as a single-file SVG/HTML animation"). It ran for 12 minutes with many tool calls and file edits, and finished with no 422 and no reconnects. On 0.1.2782 the same prompt failed after the first tool call: 422 at messages[2].assistant.reasoning_content, with real reasoning text this time, not the " " placeholder.

Real Mistral API, through the in-process gateway (/v1/responses, isolated config):

turn 1 turn 2 (function_call + output sent back) tool turn with no reasoning item
main 200 422 extra_forbidden at messages[1].assistant.reasoning_content 422
with fix 200 200 200

Raw /v1/chat/completions to Mistral, to see which assistant tool-call message shapes it accepts:

assistant tool-call message result
reasoning_content: " " 422
reasoning_content: "<text>" 422
content: [thinking part "<text>"] 200
content: [thinking part " "] (what the fix sends for the placeholder) 200
field dropped 200

Unit tests: TestTranslatedReasoningAsThinkingForMistral and an extra no-reasoning-item test both fail with 422 on main and pass with the fix. The other Mistral / DeepSeek reasoning tests pass, and so does go vet. In the full ./internal/gateway suite, the same 12 tests fail on main without the fix (Windows-specific handover, socket, marshal and root-env tests), so they're unrelated.

Minor nit, not blocking: the placeholder becomes a whitespace-only thinking part. Mistral accepts it, but strings.TrimSpace(think) == "" would drop it instead.

Could one of you open a PR so the maintainers can review it?

MystryMocha avatar Nov 03 '26 04:11 MystryMocha

Fixed in dd2be6565, released in v0.1.3125.

On /v1/responses, a request translated to a Mistral upstream (preset mistral or api.mistral.ai) now goes through the same mistralThinking conversion the direct Chat route has. An earlier turn's reasoning reaches Mistral as a typed thinking part, not reasoning_content, so a Codex tool loop on mistral/deepseek-v4-1-flash-1017 no longer gets 422 extra_forbidden after its first call. A whitespace-only placeholder is now dropped, not sent as a blank thinking part (MystryMocha's nit).

Credit: the fix is @skygunner's 29b0ea308, ported with Co-authored-by. @Edward4226 sent an independent patch of the same shape (0b8aee26f), and @MystryMocha verified it end to end with the real Mistral API and real Codex. Thank you all.

I'm leaving this open until the reporter confirms it works on v0.1.3125.

yetone avatar Nov 06 '26 06:11 yetone

Confirmed working. I tested on v0.1.3148, which includes dd2be6565, rather than on v0.1.3125 itself.

Setup: real Codex (codex-cli 0.166.0) on mistral/deepseek-v4-1-flash-1017, effort max, over /v1/responses, against the real Mistral API.

  • A short run with two separate shell tool calls: 3 requests, all 200, and the final answer was correct.
  • A real coding task in one of my own projects: 48 requests over about 15 minutes, all served by api.mistral.ai with 200. About 36k reasoning tokens went through (up to 6.8k in one request), with context reaching about 208k tokens. No fallback to another provider and no 422 extra_forbidden.

Before the fix, the request after the first tool call failed.

Thanks @yetone, @skygunner and @Edward4226. This can be closed from my side.

MystryMocha avatar Nov 06 '26 11:11 MystryMocha

Closing as completed.

Fixed in dd2be6565, released in v0.1.3125. I confirmed it on v0.1.3148 against the real Mistral API, using real Codex on mistral/deepseek-v4-1-flash-1017 over /v1/responses. A short run and a 48-request coding task were all 200. There was no 422 extra_forbidden after the first tool call, and no request fell back to another provider. Details are in my comment above.

Thanks again @yetone, @skygunner and @Edward4226.

MystryMocha avatar Nov 13 '26 17:11 MystryMocha