diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 52379141b..e823eb271 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -350,8 +350,12 @@ var anthropicModel = regexp.MustCompile(`(?i)(?:^|[/.:-])claude-`) // GLM-5.3 answers 1210, "…always engages in thinking…", or, in Chinese // (ZCode's GLM-5.3-Flash), "该模型始终支持思考,不可关闭" (#699); DashScope's // own glm-5.3 answers "The value of the enable_thinking parameter is -// restricted to True." -var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking (?:can ?not|can't) be (?:disabled|turned off)|enable_thinking[^"]{0,60}restricted to true|始终(?:支持|开启|启用)?思考|思考[^"]{0,20}(?:不可|无法|不能)关闭`) +// restricted to True." MiniMax's MiniMax-M3.1-Flash-Preview answers +// "…requires adaptive thinking; thinking.type="disabled" (including +// reasoning.effort=none) is not allowed (2013)", and Volcengine Ark's +// glm-5.3 "thinking.type `disabled` is not supported by this model" +// (#1141). +var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking (?:can ?not|can't) be (?:disabled|turned off)|enable_thinking[^"]{0,60}restricted to true|始终(?:支持|开启|启用)?思考|思考[^"]{0,20}(?:不可|无法|不能)关闭|requires adaptive thinking\W+thinking\.type\W+disabled\W+\(including reasoning\.effort=none\) is not allowed|thinking\.type \x60disabled\x60 is not supported by this model`) // ThinksOnlyWhenAsked is a model that takes thinking turned off on the // Messages API whatever its levels: one of Anthropic's own (anthropicModel, diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index bce019cf2..9a9a91e86 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -669,6 +669,48 @@ func TestAnthropicPassthroughAlwaysThinksInChinese(t *testing.T) { } } +// #1141: MiniMax and Volcengine Ark refuse thinking turned off in their +// own words, the reporters' literal messages. The request magpie turned it +// off for (Claude Desktop's model_validation probe, its health probe and +// titles send none) is asked again with thinking left to the model, once; +// thinking the caller asked for goes as it came, and another 400 on the +// same request, or a near wording, is the caller's to see. +func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) { + for _, c := range []struct{ vendor, refusal string }{ + {"MiniMax", `{"type":"error","error":{"type":"invalid_request_error","message":"invalid params, model \"MiniMax-M3.1-Flash-Preview\" requires adaptive thinking; thinking.type=\"disabled\" (including reasoning.effort=none) is not allowed (2013)"}}`}, + {"Ark", `{"type":"error","error":{"type":"invalid_request_error","message":"thinking.type ` + "`disabled`" + ` is not supported by this model Request id: 021760000000000000000000000000000000000000000000000000"}}`}, + } { + t.Run(c.vendor, func(t *testing.T) { + f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`} + f.refuse = func(b []byte) (int, string) { + if bytes.Contains(b, []byte(`"disabled"`)) { + return 400, c.refusal + } + return 0, "" + } + setup(t, provider.Anthropic, f) + code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":[{"type":"text","text":"Hi","cache_control":{"type":"ephemeral"}}]}]}`) + if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"Hi"`)) { + t.Fatalf("omitted: %d %s after %d calls, last sent %s", code, body, f.calls, f.got) + } + f.calls = 0 + if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"."}]}`); code != 200 || f.calls != 1 || !bytes.Contains(f.got, []byte(`"thinking":{"type":"adaptive"}`)) { + t.Errorf("adaptive: %d %s after %d calls, sent %s", code, body, f.calls, f.got) + } + for _, other := range []string{ + `{"type":"error","error":{"type":"invalid_request_error","message":"invalid params, max_tokens must be less than 8192 (2013)"}}`, + `{"type":"error","error":{"type":"invalid_request_error","message":"top_k is not supported by this model Request id: 0217"}}`, + } { + f.calls = 0 + f.refuse = func([]byte) (int, string) { return 400, other } + if code, _ := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":"."}]}`); code != 400 || f.calls != 1 { + t.Errorf("%s: %d after %d calls", other, code, f.calls) + } + } + }) + } +} + func TestOpenRouterMandatoryReasoningRetries(t *testing.T) { const refusal = `{"error":{"message":"OpenRouter: Reasoning is mandatory for this endpoint and cannot be disabled.","type":"invalid_request_error"},"type":"error"}` for _, tc := range []struct { diff --git a/internal/gateway/wire_effort_test.go b/internal/gateway/wire_effort_test.go index d16469326..f8da058e8 100644 --- a/internal/gateway/wire_effort_test.go +++ b/internal/gateway/wire_effort_test.go @@ -2,6 +2,7 @@ package gateway import ( "encoding/json" + "fmt" "net/http/httptest" "strings" "testing" @@ -92,6 +93,17 @@ func TestTranslatedEffortLoggedAsSent(t *testing.T) { // (#1636): asked again thinking at its least, as before, and from then on // asked so at once. func TestTranslatedThinkingOffRefused(t *testing.T) { + for i, refusal := range []string{ + `GLM-5.3 always engages in thinking and cannot be disabled`, + // MiniMax's and Volcengine Ark's wordings (#1141) + `invalid params, model \"MiniMax-M3.1-Flash-Preview\" requires adaptive thinking; thinking.type=\"disabled\" (including reasoning.effort=none) is not allowed (2013)`, + "thinking.type `disabled` is not supported by this model Request id: 0217", + } { + t.Run(fmt.Sprint(i), func(t *testing.T) { translatedThinkingOffRefused(t, refusal) }) + } +} + +func translatedThinkingOffRefused(t *testing.T, refusal string) { var sent []string f := &fake{t: t, reply: anthropicHi} f.refuse = func(b []byte) (int, string) { @@ -103,7 +115,7 @@ func TestTranslatedThinkingOffRefused(t *testing.T) { json.Unmarshal(b, &v) sent = append(sent, v.Thinking.Type) if v.Thinking.Type == "disabled" { - return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"GLM-5.3 always engages in thinking and cannot be disabled"}}` + return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"` + refusal + `"}}` } return 0, "" }