Files
magpie/internal/gateway/idless_reasoning.go
T
yetone 3f4372cc9e gateway: a DeepSeek channel that refuses reasoning without an id is asked again without it, and its 422 moves a group on (yyhhyyyyyy on X)
Codex continuing on a DeepSeek channel's Responses API, after a reply
that had no reasoning, sent a step with nothing to replay. magpie leads
such a step with a reasoning item holding " " and no id (noReasoningItem,
reasonFirst), which DeepSeek's own /v1/responses wants: without it it
refuses the request with "The `reasoning_text` in the thinking mode must
be passed back to the API" (#1822, #1592, #1687, checked on
api.deepseek.com 2026-10-12). The reporter's channel turns that item
away with 422 "Input tag 'reasoning' found using 'type' does not match
any of the expected tags: 'message', 'tool_result'", pydantic's words
for a tagged-union member it has no type for. It takes a reasoning item
with an id (its own, which Codex sends back), so only the id-less ones,
the " " and a translated turn's reasoning text, are refused. The 422
wasn't retryable, so a group stopped at that member.

What changed:
- idless_reasoning.go: idlessReasoningRefusal matches that message;
  withoutIDlessReasoning drops reasoning input items without an id.
- gateway.go, relayed Responses: on that refusal the request is asked
  once more without them, and once taken the provider/model is marked
  (idlessReasoningRefused) and not sent one again. Items with an id go.
- gateway.go, translated to Responses: the same, through the build
  loop (mark, rebuild, drop).
- fallback.go: "does not match any of the expected tags" is a shape
  refusal (shapeWords), the same kind as xAI's "unknown item type": a
  group's next member (DeepSeek's own API takes the item) is asked, and
  the member doesn't rest. This only matters when the retry without the
  items is refused too, or the member can't drop any.
- The placeholder itself is unchanged: an upstream that never refused it
  (DeepSeek's own API) still gets it, so #1822 stays fixed.
- docs/subsystems/gateway-routing.md: the Responses replay paragraph.

Fixtures are the reporter's message, word for word, in FastAPI's detail
envelope and in OpenAI's error object (the reporter gave the message,
not the envelope).

Run:
- New tests: TestIdlessReasoningRefusedByChannel (relayed, both
  envelopes, second request taken at once),
  TestTranslatedIdlessReasoningRefusedByChannel (a Chat client
  translated), TestGroupFailsOverIdlessReasoningRefusal,
  TestIdlessReasoningKeptForDeepSeek, TestWithoutIDlessReasoning.
- Broken by hand: with gateway.go and fallback.go at origin/main
  (idless_reasoning.go kept so the tests compile), the three refusal
  tests fail with the reporter's 422 reaching the client;
  TestIdlessReasoningKeptForDeepSeek passes, as it guards #1822.
- mote: go test ./internal/gateway -run with the tests above plus
  TestDeepSeekStepSaysBeforeItsCall, TestGroupFailsOverARequestShape,
  TestTranslatedDeepSeek*, TestWithReasoningText*: ok.
- mica (mote had a full suite running, load 24): go test -count=1
  -timeout 45m ./... at 29cb2262a (this change on origin/main 31dcfcc33):
  103 packages ok, exit 0. GOOS=linux and GOOS=windows go vet -tags
  nogui ./...: ok. gofmt -l: clean.
- Rebased onto a5b797d8d (main gained image caps in internal/gateway
  and provider, and sessions timings): mica, go test -count=1 -timeout
  45m ./internal/gateway ./internal/provider ./internal/sessions at the
  pushed tree: ok, ok, ok. gofmt -l: clean. The full suite above ran
  at the earlier base. be6f8b730 (Zen through Bun: internal/provider
  zen_bun.go, internal/netproxy) landed after that run and touches no
  file here; it was not run again with this change.

Not tried: the reporter's channel itself (its URL isn't known), so the
id rule rests on the reporter's reading and their error text. A built
magpie end to end.
2026-11-17 16:14:13 +08:00

61 lines
2.0 KiB
Go

package gateway
import (
"encoding/json"
"github.com/yetone/magpie/internal/lazyre"
)
// idlessReasoningRefusal is an upstream's Responses API turning a request
// away over a reasoning input item it reads as neither a message nor a
// tool's result: a DeepSeek channel's 422 "Input tag 'reasoning' found
// using 'type' does not match any of the expected tags: 'message',
// 'tool_result'" (yyhhyyyyyy on X), pydantic's words for a tagged union
// whose tag it has no member for. It takes a reasoning item with an id
// (one it gave, which Codex sends back), and refuses one without: the
// " " a step leads with when it has none (noReasoningItem, reasonFirst),
// or a translated turn's reasoning text (buildResponses), neither of which
// has an id.
var idlessReasoningRefusal = lazyre.New(`(?i)input tag \\?['"]?reasoning\\?['"]? found using \\?['"]?type\\?['"]? does not match any of the expected tags`)
// idlessReasoningRefused is how unfit remembers a provider turning away,
// for model, a reasoning item without an id (withoutIDlessReasoning).
func idlessReasoningRefused(model string) string { return "idless reasoning\x00" + model }
// withoutIDlessReasoning is a Responses body without its reasoning input
// items that carry no id, for an upstream that refused one
// (idlessReasoningRefusal). A body with none comes back as it is.
func withoutIDlessReasoning(body []byte) []byte {
if !bodyHas(body, `"reasoning"`) {
return body
}
var q map[string]json.RawMessage
if decodeJSONView(body, &q) != nil {
return body
}
items, isList := inputList(q["input"])
if !isList {
return body
}
kept := make([]json.RawMessage, 0, len(items))
for _, raw := range items {
var it struct {
Type string `json:"type"`
ID string `json:"id"`
}
if decodeJSONView(raw, &it) == nil && it.Type == "reasoning" && it.ID == "" {
continue
}
kept = append(kept, raw)
}
if len(kept) == len(items) {
return body
}
q["input"], _ = marshalJSON(kept)
nb, err := marshalJSON(q)
if err != nil {
return body
}
return nb
}