magpie icon indicating copy to clipboard operation
magpie copied to clipboard

[Bug] gateway/Kimi: K3.1 Pro 400s on every request without a `thinking` field (ZCode's `output_config.effort` shape) — refusal wording not in `alwaysThinks`

Open CuteSamurai24 opened this issue 1 month ago • 9 comments

What happens

kimi-code-cn/k3.1-pro returns 400 on every request that does not carry a thinking field. ZCode is such a client: it asks for a level as output_config.effort, and sends no thinking at all, so every turn fails and the session cannot start.

provider=magpie model=kimi-code-cn/k3.1-pro reason=invalid_request status=400 retryable=false
Kimi Code (China): K3.1 Pro does not support disabled thinking mode.
Please enable thinking and try again, or choose another model.

This is the same failure family as #1141 — thinkingOffUnlessAsked adds "thinking":{"type":"disabled"} to a Messages request that doesn't mention thinking, the upstream refuses it, and the retry at refusesThinkingOff does not fire because Kimi's wording is not among the recognised ones. #1141 added MiniMax's and Volcengine Ark's wordings; Kimi's K3.1 wording (does not support disabled thinking mode) is a third one, and it is not matched.

k3.1-pro-highspeed fails identically.

Environment

  • Windows x64, magpie 0.1.1593 (also on current main 5a94eec3).
  • Provider: built-in Kimi Code (China), anthropic endpoint https://api.kimi.com/coding.
  • Client: ZCode, provider magpie over anthropic-messages at http://127.0.0.1:3425.
  • The request ZCode actually sends (from its own model-io log), abridged:
{"model":"kimi-code-cn/k3.1-pro","max_tokens":128000,"system":[...],"messages":[...],
 "tools":[...],"tool_choice":{"type":"auto"},"stream":true,
 "output_config":{"effort":"max"}}

No thinking key. That is the whole trigger.

Repro

curl -s -X POST http://127.0.0.1:3425/v1/messages \
  -H "Authorization: Bearer $KEY" -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"kimi-code-cn/k3.1-pro","max_tokens":64,
       "messages":[{"role":"user","content":"say ok"}]}'
# → 400 Kimi Code (China): K3.1 Pro does not support disabled thinking mode.

Adding one field makes it pass, and output_config alone does not:

caller's body (via magpie) result
no thinking (ZCode's shape) 400
output_config:{"effort":"high"}, no thinking 400
output_config:{"effort":"max"}, no thinking 400
thinking:{"type":"disabled"} 400
thinking:{"type":"enabled","budget_tokens":1024} 200, thinks
thinking:{"type":"adaptive"} 200, thinks
thinking:null 200, thinks
group/use4reset (routes to the same model), no thinking 200, thinks

Direct-upstream control (no magpie)

Same body, straight to https://api.kimi.com/coding/v1/messages with the provider's own key:

caller's body (direct) result
no thinking 200, 199 chars of thinking
thinking:{"type":"disabled"} 400 (the same wording)
output_config:{"effort":"high"}, no thinking 200, thinks

So Kimi accepts the omitted field and only refuses the explicit disabled. Magpie's injected field is what turns a working request into a 400 — the same conclusion as #1141's Ark table, and the same root cause.

Why the self-heal doesn't fire

On current main 5a94eec3:

  • thinkingOffUnlessAsked (internal/gateway/anthropic.go:260) appends "thinking":{"type":"disabled"}; called at internal/gateway/gateway.go:4495.
  • The retry is internal/gateway/gateway.go:4842 (withoutThinkingOff(body) && refusesThinkingOff(b)) and :5485 (Anthropic, req.ThinkOff, 400).
  • refusesThinkingOff (anthropic.go:404) consults alwaysThinks (anthropic.go:395), whose alternatives are: always engages in thinking, thinking (?:can ?not|can't) be (?:disabled|turned off), enable_thinking…restricted to true, the two Chinese phrasings, MiniMax's requires adaptive thinking…, and Ark's thinking.type `disabled` is not supported by this model.
  • Kimi answers K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model. — matched by none of them. Confirmed by running the exact regex from anthropic.go:395 against the live error body: no match.

withoutThinkingOff would have handled it; only the match misses, exactly as #1141 described.

Suggested minimal fix, in the spirit of #1141: add Kimi's wording to alwaysThinks, e.g.

|does not support disabled thinking mode

and a regression test from the real error body. Acceptance checks I can re-run on a release: omitted thinking succeeds, adaptive untouched, an unrelated 400 does not retry.

Second symptom: the same injection silently costs thinking on models that accept off

Not an error, so the retry above cannot help it. k3-256k (and k3) accept the injected disabled and answer 200 — but with no thinking at all, and the output_config.effort the client asked for is moot.

Same prompt, one Messages request each:

model body direct api.kimi.com via magpie
k3-256k no thinking, no output_config 200, 102 thinking tokens 200, 0
k3-256k output_config:{"effort":"max"}, no thinking 200, 147 thinking tokens 200, 0

Kimi's K3 models think by default on their Anthropic endpoint, which thinkingOffUnlessAsked's own comment acknowledges ("DeepSeek and other vendors' Anthropic endpoints think by default"). For a client that asks for output_config.effort and no thinking, forcing thinking off discards the level it asked for rather than honouring it. If that is intended, it would help to say so; if not, it may be worth not injecting disabled when the body carries output_config.effort.

(k3.1-pro cannot show this row, because it 400s instead. When thinking is present, output_config.effort is forwarded and honoured on it: enabled + effort=low → 34 thinking tokens, enabled + effort=max → 55.)

Workaround I used and reverted

magpie model efforts kimi-code-cn/k3.1-pro off makes the 400 go away (no reasoning field is sent), but it also flattens the level picker for that model, so it is not a fix. Reverted with --reset.

Expected

  1. A Kimi Code refusal of thinking turned off is recognised, and the request is asked again with thinking left to the model (as #1141 does for MiniMax and Ark), on both the Anthropic passthrough and the translated paths.
  2. Optionally: a request that names output_config.effort is not forced to thinking off.

No credentials, session text, or raw logs are included above. The direct-upstream rows were run once each with this install's own Kimi key; the magpie rows are reproducible with the curl above.

CuteSamurai24 avatar Oct 26 '26 11:10 CuteSamurai24

Taking this. Plan: extend the alwaysThinks alternatives in internal/gateway/anthropic.go with Kimi's refusal wording (does not support disabled thinking mode) so refusesThinkingOff fires and the existing retry-with-thinking-left-to-the-model path handles it, same shape as #1141 did for MiniMax and Ark. Regression test will use the real error body. The optional output_config.effort injection question (expected #2) is a behavior change on a different surface — I'll leave it out of this patch and note it separately.

Patch will be posted in this thread when green.

written with GLM-5.3-Flash (ZCode)

ggbdpq avatar Oct 26 '26 12:10 ggbdpq

One-line patch following #1141's pattern: Kimi Code (China)'s refusal of thinking turned off joins alwaysThinks, so the request is asked again with thinking left to the model.

Root cause. thinkingOffUnlessAsked (internal/gateway/anthropic.go) appends "thinking":{"type":"disabled"} to a Messages request that mentions no thinking — ZCode's shape, which asks for a level as output_config.effort and sends no thinking key. Kimi's K3.1 models (k3.1-pro, k3.1-pro-highspeed) refuse that with 400, K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model. — a wording matched by none of alwaysThinks, mandatoryReasoning or thinkingOffRefused, so the self-heal at refusesThinkingOff never fired and every thinking-less request came back 400. As in #1141: withoutThinkingOff would have handled it; only the match missed. Kimi's sentence is the third vendor wording of that family, after MiniMax's and Volcengine Ark's.

Fix. One literal alternative added to alwaysThinks (does not support disabled thinking mode), with the doc comment updated. Both retry sites consult it: the relayed Messages path (withoutThinkingOff, gateway.go) and the translated Chat/Responses path (thinkingBackOn). Blast radius is closed: alwaysThinks has exactly one consumer, refusesThinkingOff, which runs only on 400 error bodies of the two retry sites — normal responses never pay for the match, and a 400 that contains this sentence is by definition a refusal of thinking turned off.

Verification (go1.26.8, darwin/arm64, -tags nogui):

check result
new TestAnthropicPassthroughKimiThinkingOffRefused, unpatched 1bb8143b FAIL — 400 handed back after 1 call, the injected "thinking":{"type":"disabled"} on the wire
new test + Kimi row in TestTranslatedThinkingOffRefused, patched PASS — omitted thinking 200 after one retry without thinking; adaptive goes once, as sent
unrelated 400s not retried PASS — Kimi's own max_tokens 400 and near wording tool use is not supported by this model stay 400, 1 call
ablation: regex change reverted to 1bb8143b, tests kept FAIL again, same 400-handed-back (both tests)
restored PASS
full go test -tags nogui ./internal/gateway ok (364s), incl. the #1141 MiniMax/Ark and Chinese-escaped siblings
go test -tags nogui ./internal/catalog ./internal/codexcat ok
gofmt -l internal/gateway clean
go vet ./... darwin / GOOS=linux -tags nogui / GOOS=windows clean (darwin prints the pre-existing cgo -Wenum-conversion warning in internal/gui, files untouched here)
go build -tags nogui darwin / linux / windows ok
internal/provider suite pre-existing fatal (close of synctest channel from outside bubble), reproduces on clean 1bb8143b — unrelated to this change

Not tried against the live Kimi endpoint; the error body is the reporter's, as in #1141.

Two notes. The reporter's second symptom (k3-256k accepts the injected disabled and answers with zero thinking even though output_config.effort was asked) is a different surface — the injection decision, not the refusal match — and is deliberately left out of this patch; the retry cannot help it, since it is a 200. And main has moved to 6e6aba51 since 1bb8143b, touching none of the three files this patch edits, so git am applies cleanly there.

Attribution: AI-written; I've read the whole diff and the tests pass. Please keep the author (ggbdpq) or add Co-authored-by: ggbdpq <[email protected]>.

patch: gateway: Kimi Code's refusal of thinking turned off is asked again with it left to the model (#3739) — git am applies it as is; SHA-256 first 16: db4e4f826064f6f9
From 9984a235b6a3392bbed069a475fc82fde89ab3cb Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Mon, 26 Oct 2026 20:38:17 +0800
Subject: [PATCH] gateway: Kimi Code's refusal of thinking turned off is asked
 again with it left to the model (#3739)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

thinkingOffUnlessAsked turns thinking off on a Messages request for a
non-Claude model that doesn't mention it. Kimi Code (China)'s K3.1
(k3.1-pro, k3.1-pro-highspeed) refuses that with 400, "K3.1 Pro does not
support disabled thinking mode. Please enable thinking and try again, or
choose another model.", in a wording neither alwaysThinks,
mandatoryReasoning nor thinkingOffRefused matched, so the 400 went back
to the client on every request that carried no thinking — ZCode's, which
asks for a level as output_config.effort and sends no thinking — and the
session could not start. withoutThinkingOff would have handled it; only
the match missed, as in #1141.

alwaysThinks now matches Kimi's literal sentence. Both retry sites use
it: the relayed Messages path (withoutThinkingOff, gateway.go) and the
translated Chat/Responses path (thinkingBackOn).

Tests: TestAnthropicPassthroughKimiThinkingOffRefused (the reporter's
error body: omitted thinking succeeds after one retry without thinking;
adaptive goes once, as sent; Kimi's own max_tokens 400 and a near wording
"tool use is not supported by this model" are not retried), and
TestTranslatedThinkingOffRefused now runs Kimi's wording. With the regex
change reverted both fail (400 handed back).

Fixes #3739

Generated-by: GLM-5.3-Flash (ZCode)
---
 internal/gateway/anthropic.go        |  5 ++--
 internal/gateway/gateway_test.go     | 36 ++++++++++++++++++++++++++++
 internal/gateway/wire_effort_test.go |  2 ++
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go
index 3f346ad5..94363a2a 100644
--- a/internal/gateway/anthropic.go
+++ b/internal/gateway/anthropic.go
@@ -391,8 +391,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 1329a40c..4495dcb1 100644
--- a/internal/gateway/gateway_test.go
+++ b/internal/gateway/gateway_test.go
@@ -711,6 +711,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 712fb854..f7e9666d 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
-- 
2.50.1 (Apple Git-155)

Branch with the commit: hunt90-v2-3739-kimi-refusal (head 9984a235b6a3392bbed069a475fc82fde89ab3cb).

ggbdpq avatar Oct 26 '26 12:10 ggbdpq

Still reproducible on 2026-10-30 with Magpie 0.1.2143 and ZCode 3.17.1 on Windows x64 (10.0.26200), using the built-in kimi-code-cn provider and kimi-code-cn/k3.1-pro.

A fresh ZCode session fails with the same 400 after about 1.2 seconds. ZCode calls Magpie over anthropic-messages at http://127.0.0.1:3425; its model-io record contains this abridged body:

{
  "model": "kimi-code-cn/k3.1-pro",
  "max_tokens": 128000,
  "tool_choice": {"type": "auto"},
  "stream": true,
  "output_config": {"effort": "max"}
}

There is no thinking key. The upstream error is unchanged:

Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.

The direct-upstream control also still holds on these versions: ZCode -> https://api.kimi.com/coding with k3.1-pro and the same output_config.effort=max succeeds, while the request through Magpie fails.

I have not tested 0.1.2146; the 0.1.2144–0.1.2146 release notes do not mention this fix. Since the proposed one-line alwaysThinks patch above is still not in this release, could it be merged and released? I can re-test the same route on a fixed build. No credentials or session content included.

zfsdream96 avatar Oct 30 '26 06:10 zfsdream96

Landed in 72e8d9fa6, tagged v0.1.2216. The fix is by @ggbdpq, with authorship kept, brought over by gua2100.

alwaysThinks now matches Kimi's wording, "does not support disabled thinking mode". A request with no thinking (ZCode's, which asks for a level as output_config.effort) that gets this 400 is now sent again once without "thinking":{"type":"disabled"}. Later requests to that model skip the disabled setting from the start. Both paths are covered: relayed Messages and translated Chat/Responses.

What was run:

  • I sent the curl body from this issue through the real binary in a sandbox HOME, to a fake Anthropic upstream that returns Kimi's literal 400. Before the fix it came back as a 400. Now it is a 200 after one retry.
  • The ZCode request shape (effort max, no thinking): the same before and after.
  • The tests fail with the regex change reverted.

Not tried: the real Kimi Code endpoint and the kimi-code-cn preset itself. Also not addressed: the second symptom here, where k3 loses thinking when output_config.effort is asked for. Leaving this open: could you confirm on v0.1.2216 once it's out?

yetone avatar Oct 30 '26 13:10 yetone

Patch author here (the Kimi thinking-off fix, 72e8d9fa6). Confirming it shipped in v0.1.2216 (and is carried by the current v0.1.2277 as well).

Verification on the v0.1.2277 tree (macOS, go test -tags nogui -count=1 ./internal/gateway), the thinking-off passthrough family: TestAnthropicPassthroughKimiThinkingOffRefused, TestTranslatedThinkingOffRefused, plus the neighbours TestAnthropicPassthroughTurnsThinkingOffUnlessAsked, TestAnthropicPassthroughDashScopeThinkingRestricted, TestAnthropicPassthroughMiniMaxArkThinkingOffRefused — 5/5 pass, output ends ok github.com/yetone/magpie/internal/gateway 1.073s.

From the patch side this is done — happy for this to close once the reporter confirms.

ggbdpq avatar Oct 30 '26 22:10 ggbdpq

The same refusal on the Chat side (a Chat client's reasoning_effort none relayed to Kimi Code, or Codex/Claude Desktop's none translated to its Chat endpoint) was still handed back; 59546b3fd (v0.1.2332) asks it again at the model's lowest and so from then on. The Messages path (ZCode's default) has had the fix since v0.1.2216. Not tried against the real Kimi Code endpoint.

yetone avatar Oct 31 '26 05:10 yetone

Reproducing on 0.1.4332 (well after the v0.1.2216 Messages fix and the v0.1.2332 Chat fix), Windows 11 x64 — so this looks like a possible regression, or Kimi changed behaviour again.

Route here is the Chat endpoint (/v1/chat/completions against https://api.kimi.com/coding/v1), not the anthropic/messages path in the original report. Model kimi/k3.1-pro, reached through a routing group (smart-split → strong → first member kimi/k3.1-pro).

Timeline from the local gateway routing log (routing/2026-11-16.jsonl.gz and 2026-11-17.jsonl):

  • 2026-11-16 (all day): 208 calls to api.kimi.com / k3.1-pro, all 200, zero 400.
  • 2026-11-17 00:23 onward: first 400; since then 414 × 400 (plus 117 × 405 from ZCode risk-control, which is unrelated).

The error text is identical to the original report:

Kimi Code: K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.

One thing that may differ from the original analysis: the failures are not only no-thinking requests. effort distribution of the 400s — minimal 226, high 174, no-effort 27, none 1 — so a substantial number of effort=high requests (which should think) return this too, not just minimal/no-thinking ones. Worth a look in case Kimi now also rejects the omitted thinking field, in which case the existing "ask again without disabled" self-heal would itself fail.

No credentials or session content included.

fatinghenji avatar Nov 16 '26 17:11 fatinghenji

Follow-up: upgraded 0.1.4332 → 0.1.4334 and restarted (gateway pid restarted 2026-11-17 01:11:31 +08:00). Still reproducing.

Post-restart traffic to api.kimi.com / k3.1-pro on the Chat endpoint, from the routing log:

  • 1 × 200 (the very first call after restart, effort=high)
  • 9 × 400 within the next ~2 minutes, identical wording:
Kimi Code: K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.

The 400s span all effort levels again — minimal, high, and requests with no effort field — including several explicitly effort=high ones that should think. The single 200 right after restart was also effort=high, so it is not a clean signal that high-effort requests pass.

That effort=high requests get told thinking is disabled remains the odd part, and now it survives the latest release. If the self-heal retries and still gets 400 (Kimi refusing the omitted thinking field now, not just the explicit disabled), the retry would loop once and hand back the error — which matches what the log shows.

Happy to run any targeted curl against the live endpoint (omitted thinking vs thinking:{"type":"enabled",...}) if that helps narrow it down.

fatinghenji avatar Nov 16 '26 17:11 fatinghenji

Thanks for the detailed report. I reproduced this against a live gateway and found one magpie-side gap, separate from the two fixes already merged (72e8d9fa6, 59546b3fd).

The gap. A Chat client's reasoning_effort: minimal is fitted to the model's lowest on-level (low) when the model's own list doesn't name minimal, before the body goes out. Kimi Code's K3.1 Pro answers that fitted level with the same "does not support disabled thinking mode" 400 it gives an explicit disabled. The self-heal in passthrough read the effort off the body it sent, saw an on-level, and skipped, so the 400 went back (or a routing group fell through to its next member). none, whose body stays off, self-healed all along.

The fix. The self-heal now also fires when the effort the agent asked was off (offEffort(asked)), even though the fitted body went out at an on-level, and marks reasoning off refused as it does for none. The retry is the model's lowest on-level (onEffort). An on-level the client asked for itself (high, low) is unchanged: it is not off, so it is not asked again.

Verified. New TestKimiChatMinimalFittedLevelRefused fails without the change (400 handed back after one call), passes with it (low,low: asked again once). gofmt clean; go vet / go build (-tags nogui, linux + windows) clean. The full ./internal/gateway suite's ten failures are pre-existing on this Windows host and fail the same on the base.

On the high/low refusals in the follow-up: those look upstream. Ten of ten high requests succeeded against the live endpoint while reproducing this, and the wording refuses thinking off, not an on-level. No magpie change makes an upstream accept an on-level it turns away.

I can't open a PR from this account, so here is the patch. git am applies it as is; the branch is also on fatinghenji/magpie at fix/kimi-minimal-thinking-off.

patch
From 4c1d4a3cdbf7137754a0f42b7fae00147330a7fe Mon Sep 17 00:00:00 2001
From: VantHoff <[email protected]>
Date: Wed, 18 Nov 2026 03:24:40 +0800
Subject: [PATCH] gateway: Kimi Code's K3.1 Pro asked minimal on Chat is asked
 again at its lowest, not handed back (#3739)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

A Chat client's reasoning_effort minimal is fitted to the model's lowest on
level (low) when the model's own list doesn't name minimal, before the body
goes out. Kimi Code's K3.1 Pro answers that fitted level "Kimi Code: K3.1 Pro
does not support disabled thinking mode." — the same wording it gives an
explicit disabled. The self-heal in passthrough read the effort off the body
it sent, saw an on level, and skipped: the 400 went back to the client, or a
routing group fell through to its next member. none, whose body stays off,
self-healed all along.

The self-heal now also fires when the effort the agent asked was off
(offEffort(asked)) even though the fitted body went out at an on level, and
marks reasoning off refused as it does for none. The retry is the model's
lowest on level (onEffort), as for none. An on level the client asked for
itself (high, low) is unchanged: it is not off, so it is not asked again.

The reporter's other observation — high and low refused with the same wording
on the Chat endpoint — is upstream: ten of ten high requests succeeded
against the live endpoint while reproducing this, and the wording refuses
thinking off, not an on level. No magpie change makes an upstream accept an
on level it turns away; this patch fixes only the fitted-off case, which is
magpie's.

Verified on Windows x64, go1.26.8, -tags nogui:
- TestKimiChatMinimalFittedLevelRefused fails without the change (400 handed
  back after one call) and passes with it (low,low: asked again once).
- go test -tags nogui -timeout 30m ./internal/gateway: my tests pass. Ten
  tests fail, all pre-existing on this Windows host (symlink privilege, TCP
  handover, root env, hub roots); they fail the same on the base with the
  change stashed, so they are not this change's.
- gofmt -l on the changed files: clean.
- go vet -tags nogui ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows
  go vet -tags nogui ./...; go build -tags nogui ./...; GOOS=linux go build
  -tags nogui ./...: all ok.
- The native GUI build (cgo/wails) was not run: no mingw on this machine.

Assisted-by: Cindy (https://cindy.app)
---
 docs/subsystems/gateway-routing.md   |  4 +++-
 internal/gateway/gateway.go          | 14 +++++++----
 internal/gateway/wire_effort_test.go | 36 ++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 19180dbc9..da25dced9 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -183,7 +183,9 @@ one, matched on the error body (`effortRequired`, on Chat and Responses,
 forwarded or translated; `TestEffortRequiredRefusalAskedAgainAtLow`), or
 turns none or minimal away in `alwaysThinks`' words, which name no levels
 (`needsReasoning`: Kimi Code's K3.1 Pro on Chat, #3739,
-`TestKimiChatThinkingOffRefused`), then at the model's lowest from the start; any
+`TestKimiChatThinkingOffRefused`), then at the model's lowest from the start —
+minimal too when the model's own list doesn't name it, so the body went out at
+its lowest on level instead (`TestKimiChatMinimalFittedLevelRefused`); any
 other 400 goes back as it came (`TestOtherBadRequestNotAskedAgainAtLow`). A session on Codex's
 own model sends them to ChatGPT as Codex sent them, whatever the config says
 (`TestCodexAppSideTurnsStayWithAnOwnModelSession`); so does a session model
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index e206a6e61..8557710f3 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -7531,17 +7531,21 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
 			}
 		}
 	}
-	if e := bodyEffort(proto, body); res.StatusCode == http.StatusBadRequest && proto != provider.Anthropic && !offRetried && (e == "" || offEffort(e)) {
+	if e := bodyEffort(proto, body); res.StatusCode == http.StatusBadRequest && proto != provider.Anthropic && !offRetried && (e == "" || offEffort(e) || offEffort(asked)) {
 		// no reasoning effort, or none, turned away by a model that needs
 		// one (a Codex title on a model other than its own asks none,
 		// cybercaius on Discord): asked again once at its lowest, low
-		// where its levels aren't known. Only that refusal: any other 400
-		// goes back as it came
+		// where its levels aren't known. A client's off level the model's
+		// own list doesn't name is fitted to its lowest on level first
+		// (minimal to low), and that level refused the same way is asked
+		// again too: the effort the agent asked decides, not the one the
+		// body went out at (#3739). Only that refusal: any other 400 goes
+		// back as it came
 		b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
 		res.Body.Close()
 		res.Body = io.NopCloser(bytes.NewReader(b))
-		if needsReasoning(e, b) {
-			if offEffort(e) {
+		if needsReasoning(e, b) || offEffort(asked) && alwaysThinks.Match(wordsOf(b)) {
+			if offEffort(e) || offEffort(asked) {
 				s.markUnfit(p.ID, offRefused(model), proto)
 			}
 			if res, err = s.forward(r.Context(), p, proto, path, p.Prepare(withModelEffort(proto, body, model, onEffort(p, model), p.Efforts(model))), r.Header); err != nil {
diff --git a/internal/gateway/wire_effort_test.go b/internal/gateway/wire_effort_test.go
index 785586954..713ea45ad 100644
--- a/internal/gateway/wire_effort_test.go
+++ b/internal/gateway/wire_effort_test.go
@@ -208,3 +208,39 @@ func TestKimiChatThinkingOffRefused(t *testing.T) {
 		})
 	}
 }
+
+// #3739: a Chat client's minimal is fitted to the model's lowest on level
+// (low) when the model's own list doesn't name minimal, and that level
+// refused in Kimi's always-thinks wording is asked again once, not handed
+// back: the effort the agent asked decides, not the one the body went out at.
+func TestKimiChatMinimalFittedLevelRefused(t *testing.T) {
+	const refusal = `{"error":{"message":"Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.","type":"invalid_request_error"}}`
+	var sent []string
+	f := &fake{t: t, reply: chatHi}
+	f.refuse = func(b []byte) (int, string) {
+		var v struct {
+			Effort string `json:"reasoning_effort"`
+		}
+		json.Unmarshal(b, &v)
+		sent = append(sent, v.Effort)
+		if len(sent) == 1 {
+			return 400, refusal
+		}
+		return 0, ""
+	}
+	setup(t, provider.Chat, f)
+	// the model's levels name no minimal, so the client's minimal is fitted
+	// to low before it goes out
+	if err := provider.SetModelEfforts("fake/m1", []string{"none", "low", "high"}); err != nil {
+		t.Fatal(err)
+	}
+	s := New()
+	rec := httptest.NewRecorder()
+	s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}],"reasoning_effort":"minimal"}`)))
+	if rec.Code != 200 {
+		t.Fatalf("%d %s", rec.Code, rec.Body)
+	}
+	if strings.Join(sent, ",") != "low,low" {
+		t.Errorf("reasoning_effort sent %v, want low,low (asked again once)", sent)
+	}
+}
-- 
2.56.0.windows.4


fatinghenji avatar Nov 18 '26 01:11 fatinghenji