Files
magpie/internal/gateway/gemini_anyof_fallback_test.go
T
yetoneandggbdpq cf15d79d7f gateway: a relay's Gemini refusing a tool's anyOf beside a description hands the request to the group's next member (#5374)
Google's Schema takes an anyOf alone: a tool whose parameters carry a
description beside an anyOf is turned away with "Unable to submit
request because `tool` functionDeclaration `parameters.response` schema
specified other fields alongside any_of. When using any_of, it must be
the only field set." Asked straight, Google points at
function_declarations[N].parameters, which shapeWords reads. A relay
(sub2api, TokenFree) passing Google's words on writes them camel-case,
the field in backticks and no path, so the 400 went back to the client
and the group's later members, a non-Gemini fallback among them, were
never asked (#5374, worksyang, with the two-channel comparison and the
one-line fix).

shapeWords now also reads "schema specified other fields alongside
any_?of" (any_of and anyOf). The next member is asked once and the relay
doesn't rest (failShape), as for every shape refusal. A plain "messages:
field required" and Google's "Invalid function name" 400 stay the
request's.

Run (mote, testenv sandbox HOME):
- TestGroupFailsOverAnAnyOfSchemaRefused with fallback.go put back to
  4e7661adc: FAIL, gemini_anyof_fallback_test.go:39 "400 ... (a 1, b 0)"
  (the relay's 400 went to the client, b never asked). With the change:
  ok.
- go test -tags nogui -run 'AnyOf|Shape|Fallback|Retryable|Group'
  ./internal/gateway/: ok 65.5s.
- gofmt -l on both files: nothing. go vet, GOOS=linux go vet -tags nogui,
  GOOS=windows go vet on ./internal/gateway/: clean.

Not tried: a real TokenFree / sub2api relay in front of Google (no such
credentials here); the wording is the reporter's gateway log. The full
suite is not run in this commit.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 09:53:30 +08:00

65 lines
3.2 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// tokenFreeAnyOf400 is a relay's (sub2api, TokenFree) answer passing Google's
// own Gemini on, which turned a tool's parameters away over a description
// beside an anyOf, as worksyang's gateway log has it (#5374): Google's Schema
// takes an anyOf alone. A relay saying Google's words on writes them
// camel-case, the field in backticks, no path — not the
// function_declarations[N].parameters shape shapeWords' Google branch reads.
const tokenFreeAnyOf400 = `{"error":{"message":"TokenFree: Unable to submit request because ` + "`paseo_respond_to_permission`" + ` functionDeclaration ` + "`parameters.response`" + ` schema specified other fields alongside any_of. When using any_of, it must be the only field set. Learn more: https://ai.google.dev/gemini-api/docs","type":"api_error"}}`
// googleAnyOf400 is Google saying it on a request sent straight: the same
// fixed wording, no relay in front of it.
const googleAnyOf400 = `{"error":{"code":400,"message":"Unable to submit request because ` + "`paseo_respond_to_permission`" + ` functionDeclaration ` + "`parameters.response`" + ` schema specified other fields alongside any_of. When using any_of, it must be the only field set. Learn more: https://ai.google.dev/gemini-api/docs","status":"INVALID_ARGUMENT"}}`
// A relay whose Gemini turns a tool's parameters away over a description
// beside an anyOf is a member's limit, not a bad request: the group's next
// member is asked and the relay doesn't rest (#5374). Before, the route ended
// on the 400 with the later members never asked — an agent run of minutes
// died whole on the first seat's word.
func TestGroupFailsOverAnAnyOfSchemaRefused(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{400, "", tokenFreeAnyOf400}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "tokenfree", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"tokenfree/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Status != 400 || r.Tries[0].Fail != failShape || r.Tries[0].Rest != nil {
t.Fatalf("tries %+v", r.Tries)
}
for _, x := range []struct {
status int
body string
want bool
}{
{400, tokenFreeAnyOf400, true},
{422, tokenFreeAnyOf400, true},
{400, googleAnyOf400, true},
// as the reporter ran it, a plain bad request is still the
// request's, and another Google 400 naming a declaration's name
// is neither
{400, `{"error":{"message":"messages: field required"}}`, false},
{400, `{"error":{"message":"* GenerateContentRequest.tools[0].function_declarations[2].name: Invalid function name.","status":"INVALID_ARGUMENT"}}`, false},
} {
if got := shapeRefused(x.status, []byte(x.body)); got != x.want {
t.Errorf("shapeRefused(%d, %s) = %v", x.status, x.body, got)
}
}
}