gateway: a Gemini request's thinkingConfig counts as its effort (#4529)

bodyEffort read a request's reasoning effort for Chat
(reasoning_effort), Responses (reasoning.effort, or its last
configuration_update's) and Anthropic (output_config.effort), and had
no Gemini branch: a request to a Gemini native endpoint
(:generateContent, :streamGenerateContent) that asked its model to
think through generationConfig.thinkingConfig — thinkingLevel, or a
thinkingBudget — recorded no effort at all. The upstream ran at the
strength asked (the reporter's request spent 625 thinking tokens at
thinkingLevel high), but the request list, the usage and a group
rule's effort condition all saw "", where the same client through the
OpenAI-compatible endpoint shows reasoning_effort fine.

parseGemini already reads the same config into r.Effort, through
gThinking.effort (level, else budget as the level it is nearest, -1 as
medium, none for a budget of 0 or includeThoughts alone). The new
geminiBodyEffort reads it the same way — geminiCamel first, as
parseGemini does, since clients send the snake_case spellings too —
and bodyEffort's new Gemini branch returns it. requestEffort and
routeEffort come along, so the route record, the usage and group
rules all see what the client asked for. Nothing sent upstream
changes.

Writing effort back (withBodyEffort) stays without a Gemini branch:
no reporter asked for it, and it would have to fit the level to the
model's own words.

Verification on the branch's base 47173f593, macOS arm64:
- RED before the fix, GREEN after: TestGeminiRequestEffort, new, runs
  requestEffort and parseGemini on the same bodies and asks for the
  same answer — thinkingLevel high, thinking_level LOW, thinkingBudget
  512, thinking_budget 32768, -1, 0, includeThoughts alone, no
  thinkingConfig.
- go test -tags nogui ./... : ok (see the issue comment for the rest).

Generated-by: GLM-5.3-FlashX (ZCode)

Broken by hand: with the gateway.go and gemini.go hunks reverted,
TestGeminiRequestEffort is red on its level and budget cases
(requestEffort "", want "high", ...). Callers of bodyEffort checked:
withBodyEffort is a no-op for Gemini, and effortOf never returns
none or minimal, so the 5338 branch is never reached for Gemini.
This commit is contained in:
ggbdpq
2026-11-01 02:45:01 +08:00
committed by yetone
parent e89a4b3c77
commit 5662c53a02
3 changed files with 51 additions and 1 deletions
+31
View File
@@ -227,6 +227,37 @@ func TestTraceRecordsTheEffortSent(t *testing.T) {
}
}
// A Gemini request's thinkingConfig is the reasoning it asks for, read
// as parseGemini reads it (#4529): its level, or its budget as the level
// it is nearest, by either spelling of the fields' names.
func TestGeminiRequestEffort(t *testing.T) {
for _, c := range []struct {
body string
want string
}{
{`{"generationConfig":{"thinkingConfig":{"thinkingLevel":"high"}}}`, "high"},
{`{"generationConfig":{"thinkingConfig":{"thinking_level":"LOW"}}}`, "low"},
{`{"generationConfig":{"thinkingConfig":{"thinkingBudget":512}}}`, "low"},
{`{"generationConfig":{"thinkingConfig":{"thinking_budget":32768}}}`, "xhigh"},
{`{"generationConfig":{"thinkingConfig":{"thinkingBudget":-1}}}`, "medium"},
{`{"generationConfig":{"thinkingConfig":{"thinkingBudget":0}}}`, ""},
{`{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`, ""},
{`{"generationConfig":{}}`, ""},
{`{"contents":[{"parts":[{"text":"hi"}]}]}`, ""},
} {
if got := requestEffort(provider.Gemini, []byte(c.body)); got != c.want {
t.Errorf("%s: requestEffort %q, want %q", c.body, got, c.want)
}
r, err := parseGemini([]byte(c.body))
if err != nil {
t.Fatalf("%s: %v", c.body, err)
}
if r.Effort != c.want {
t.Errorf("%s: parseGemini effort %q, want %q", c.body, r.Effort, c.want)
}
}
}
// requestEffort reads the reasoning a request asks for in each API's words.
func TestRequestEffort(t *testing.T) {
for _, c := range []struct {
+4 -1
View File
@@ -7960,7 +7960,8 @@ var otherOffNamed = map[string]*regexp.Regexp{
var noneQuoted = regexp.MustCompile(`(?i)['"]none\\?['"]`)
// bodyEffort is the reasoning effort a Chat or Responses request asks for,
// or an Anthropic one in its output_config. A Responses request that
// or an Anthropic one in its output_config, or a Gemini one in its
// generationConfig's thinkingConfig. A Responses request that
// changed it mid-thread asks for its last configuration_update's
// (effortUpdated).
func bodyEffort(proto provider.Protocol, body []byte) string {
@@ -7986,6 +7987,8 @@ func bodyEffort(proto provider.Protocol, body []byte) string {
return v.Reasoning.Effort
case proto == provider.Anthropic && v.OutputConfig != nil:
return v.OutputConfig.Effort
case proto == provider.Gemini:
return geminiBodyEffort(body)
}
return ""
}
+16
View File
@@ -106,6 +106,22 @@ func (tc *gThinking) effort() string {
return ""
}
// geminiBodyEffort is the reasoning a Gemini request's generationConfig
// asks for through its thinkingConfig, as parseGemini reads it (#4529):
// the level it names, else its budget as the level it is nearest. ""
// when it asks for none, or there is no thinkingConfig.
func geminiBodyEffort(body []byte) string {
var g struct {
GenerationConfig *struct {
ThinkingConfig *gThinking `json:"thinkingConfig"`
} `json:"generationConfig"`
}
if decodeJSON(geminiCamel(body), &g) != nil || g.GenerationConfig == nil || g.GenerationConfig.ThinkingConfig == nil {
return ""
}
return g.GenerationConfig.ThinkingConfig.effort()
}
// buildGemini is a generateContent body for an upstream that speaks Gemini
// itself (Factory's /api/llm/g). The contents are the ones Code Assist
// sends inside its envelope. systemInstruction has no role: droid sends