Files
magpie/internal/gateway/mistral_extra_fields_test.go
T
yetoneandskygunner dd2be65656 gateway: a Responses request translated to Mistral carries its thinking as parts, not reasoning_content (#4906)
Codex on /v1/responses with a DeepSeek-named model served at Mistral
(preset mistral or api.mistral.ai, here deepseek-v4-1-flash-1017) broke
after its first tool call: the Chat build replays an earlier turn's
thinking in reasoning_content for any model whose name says DeepSeek,
wherever it is served (chatReplaysReasoning keys on the name), and
Mistral answers that field with 422 extra_forbidden. The #494 fix
converted it to Mistral's typed thinking part (mistralThinking) only on
the Chat passthrough route, so /v1/chat/completions on the same provider
answered 200 while every follow-up on /v1/responses was refused.

forwardTranslated now runs mistralThinking on the body built for a
Mistral upstream, with the same gate the relayed route uses. Other
upstreams keep the #1462 replay (a DeepSeek at DeepSeek still gets
reasoning_content), and the relayed route is unchanged.

MystryMocha's nit: a whitespace-only thinking (the " " placeholder the
replay gives a tool turn with no reasoning item) is now dropped instead
of being sent as a blank thinking part. This applies to the relayed
route too; Mistral accepts the dropped field (MystryMocha's raw table).

Ported from skygunner's 29b0ea308 (branch
fix/4906-mistral-translated-reasoning), on origin/main 1c45645b5.
Edward4226's 0b8aee26f was an independent fix of the same shape.

Run (all through mote-run, at this tree):
- gofmt -l internal/gateway docs: empty
- go test -tags nogui ./internal/gateway -count=1 -timeout 45m: ok (894.9s)
- GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...,
  go vet ./..., go build: ok
  Run at this change on 1c45645b5, then rebased onto 3f53fa1f3; the
  two commits between (763ea9438, 3f53fa1f3) touch GUI CSS, webfonts.js
  and GUI tests only, nothing Go.

Broken by hand:
- forwardTranslated's mistralThinking call taken out:
  TestTranslatedReasoningAsThinkingForMistral red with the reporter's
  422 at messages[1].assistant.reasoning_content ("I should read
  note.txt with the tool."), and the new
  TestTranslatedToolTurnWithoutReasoningForMistral red with the same
  422 on the " " placeholder.
- TrimSpace reverted to think == "": only
  TestTranslatedToolTurnWithoutReasoningForMistral red ("the call was
  given a blank thinking part").

Real use: not tried here, no Mistral key. MystryMocha ran skygunner's
29b0ea308 on main a92cc3f3 (Windows/amd64) against the real Mistral API
and in real Codex Desktop on mistral/deepseek-v4-1-flash-1017: a
12-minute session of many tool calls with no 422, and turn 2 and a tool
turn without a reasoning item 422 on main, 200 with the fix. The
TrimSpace change on top of it was not run against the real API; per the
same report, Mistral answers 200 with the field dropped.

Co-authored-by: skygunner <84759+skygunner@users.noreply.github.com>
2026-11-06 14:21:27 +08:00

69 lines
2.8 KiB
Go

package gateway
import (
"encoding/json"
"fmt"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// mistralRefuse answers as Mistral's chat completions do a body with a
// field it doesn't know: 422 extra_forbidden, every such field listed.
func mistralRefuse(body []byte) (int, string) {
known := []string{"model", "messages", "stream", "stream_options", "max_tokens", "temperature", "top_p", "tools", "tool_choice",
"parallel_tool_calls", "response_format", "stop", "random_seed", "presence_penalty", "frequency_penalty", "n"}
var m map[string]json.RawMessage
json.Unmarshal(body, &m)
var detail []string
for k, v := range m {
if !slices.Contains(known, k) {
detail = append(detail, fmt.Sprintf(`{"type":"extra_forbidden","loc":["body",%q],"msg":"Extra inputs are not permitted","input":%s}`, k, v))
}
}
if len(detail) == 0 {
return 0, ""
}
return 422, `{"object":"error","message":{"detail":[` + strings.Join(detail, ",") + `]},"type":"invalid_request_error","param":null,"code":null,"raw_status_code":422}`
}
// OpenCode's store:false (and Kimi Code's thinking switch) to Mistral, which
// turns away fields it doesn't know (#393): asked again without them, and
// not sent them again.
func TestMistralRefusesStore(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","model":"m1","choices":[{"delta":{"content":"ok"}}]}`,
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`), refuse: mistralRefuse}
setup(t, provider.Chat, f)
srv := New()
for _, c := range []struct {
body string
calls int
}{
{`{"model":"m1","stream":true,"store":false,"messages":[{"role":"user","content":"hi"}],"max_tokens":20}`, 2},
{`{"model":"m1","stream":true,"store":false,"messages":[{"role":"user","content":"hi"}],"max_tokens":20}`, 1},
{`{"model":"m1","stream":true,"thinking":{"type":"enabled","keep":"all"},"messages":[{"role":"user","content":"hi"}]}`, 2},
{`{"model":"m1","stream":true,"store":false,"thinking":{"type":"enabled"},"messages":[{"role":"user","content":"hi"}]}`, 1},
} {
calls := f.calls
code, body := postTo(t, srv, "/v1/chat/completions", c.body)
if code != 200 || !strings.Contains(body, `"ok"`) {
t.Fatalf("%s: %d %s", c.body, code, body)
}
if n := f.calls - calls; n != c.calls {
t.Errorf("%s: %d upstream calls, want %d", c.body, n, c.calls)
}
}
// anything else refused goes back to the client as it came
f.refuse = func([]byte) (int, string) {
return 422, `{"object":"error","message":{"detail":[{"type":"missing","loc":["body","messages"],"msg":"Field required"}]}}`
}
calls := f.calls
if code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"m1","store":false,"messages":[]}`); code != 422 || f.calls-calls != 1 {
t.Errorf("other refusal: %d after %d calls %s", code, f.calls-calls, body)
}
}