diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 12e8e358b..a5f1ba249 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -430,8 +430,9 @@ var anthropicModel = regexp.MustCompile(`(?i)(?:^|[/.:-])claude-`) // "…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`) +// (#1141). Kimi Code (China)'s K3.1 answers "…does not support disabled +// thinking mode" (#3739). +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|does not support disabled thinking mode`) // refusesThinkingOff is an Anthropic endpoint turning away a request // with thinking turned off (thinking.type disabled): a model that always diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index d8aa040c3..aceee3b8f 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -770,6 +770,42 @@ func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) { } } +// #3739: Kimi Code (China)'s K3.1 models refuse thinking turned off in +// their own words, the reporter's literal message. The request magpie +// turned it off for (ZCode sends none; it asks for a level as +// output_config.effort) 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 TestAnthropicPassthroughKimiThinkingOffRefused(t *testing.T) { + const refusal = `{"type":"error","error":{"type":"invalid_request_error","message":"Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model."}}` + 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, 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":"Kimi Code (China): max_tokens is too large"}}`, + `{"type":"error","error":{"type":"invalid_request_error","message":"Kimi Code (China): tool use is not supported by this model"}}`, + } { + 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) + } + } +} + // The same refusal, escaped as \uXXXX the way a relay whose encoder // escapes what isn't ASCII sends it (#1669): asked again all the same. func TestAnthropicPassthroughAlwaysThinksInEscapedChinese(t *testing.T) { diff --git a/internal/gateway/wire_effort_test.go b/internal/gateway/wire_effort_test.go index 712fb854b..f7e9666d0 100644 --- a/internal/gateway/wire_effort_test.go +++ b/internal/gateway/wire_effort_test.go @@ -107,6 +107,8 @@ func TestTranslatedThinkingOffRefused(t *testing.T) { // 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", + // Kimi Code's K3.1 (ZCode sends no thinking at all, #3739) + `Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.`, // Sub2API's gpt-6.1-sol reads disabled as reasoning effort none (#1465) `gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max`, // #699's Chinese, escaped as \uXXXX the way a relay's