Files
magpie/internal/gateway/gemini_schema.go
T
yetone d71362afcd gateway: a Gemini tool schema with items is an array, not its enum's string, and Antigravity gets items only on arrays (ferock)
ferock on Discord: Claude Code, model group/gemini-3-8-flash on a local
magpie whose group is served by a remote magpie's Antigravity, 400
"Remote magpie: Antigravity: GenerateContentRequest.tools[0].
function_declarations[19].parameters.properties[display].items: field
predicate failed: $type == Type.ARRAY". It worked in the morning and
broke after updating. The tool's literal schema wasn't posted, and no
Claude Code 2.1.311 built-in has a display property with items (searched
the binary), so it is an MCP tool of theirs.

Cause: geminiToolTypes (68ee4117a, v0.1.1624, 10-27 01:26) runs in
forwardOnce for any body whose model names Gemini, the local magpie's
forward to a remote magpie included. geminiSchemaType typed an untyped
schema from its enum/const values before its keywords, so
{"items":{...},"enum":["a","b"]} went on as type "string" with items
beside it. The remote's plainSchema kept the explicit type and the
items, and Google refused the whole turn. Before 68ee4117a the same node
reached plainSchema untyped and was inferred an array.

- geminiSchemaType: an object's or array's keywords (properties, items
  and the rest, keywordType, split out of schemaTypes unchanged) decide
  before enum/const values.
- plainSchema (the remote's Antigravity side, so a local magpie from
  before is served too): items/minItems/maxItems are kept only on an
  array and properties/required/min-maxProperties/propertyOrdering only
  on an object, as JSON Schema reads them; of a type list it takes the
  type its items or properties are for (["string","array"] with items is
  an array, not a string with items).
- docs/subsystems/gateway-routing.md says both.
- Sibling not changed: kimiSchemaType/collectUntypedEnums still type
  such a node from its enum; whether Kimi refuses items on a string is
  unknown, filed as #3867.

Tests (antigravity_display_items_test.go): the shape the error points to
(an untyped display with items and a string enum, plus properties with a
const) through geminiToolTypes, and through parseAnthropic +
buildCodeAssist(antigravity) as Claude Code sent it, as this version's
local passes it on, and as an older local passes it on (display typed
"string", type lists, a string with object/array keywords); every node
sent has items only as an array and properties only as an object.

Run, in a worktree at origin/main 32ea16c5a:
- go test -tags nogui ./internal/gateway -run 'CodeAssist|Codeassist|
  Gemini|Schema|Antigravity|Kimi|Plain|Grok|OneOf|Tool|Vertex|Google|
  Shape|Enum' -count=1: ok (60s)
- gofmt -l internal/gateway: clean; go vet -tags nogui (darwin),
  GOOS=linux go vet -tags nogui, GOOS=windows go vet on internal/gateway:
  clean
Broken by hand: gemini_schema.go back to origin/main,
TestGeminiToolTypesItemsBeforeEnum and TestAntigravityItemsOnlyOnArrays
FAIL (display typed "string"); codeassist.go back to origin/main,
TestAntigravityItemsOnlyOnArrays FAILs (items, minItems, properties on
type "string").

Not tried: the reporter's literal tool schema (not posted; the fixture is
the shape the error and the update point to), a real Antigravity
account, and a real two-magpie setup. Not run: the whole gateway
package or ./... (another session's full internal/gateway run was on the
machine, load ~60-75).
2026-10-27 16:34:19 +08:00

156 lines
5.3 KiB
Go

package gateway
import (
"strings"
"github.com/tidwall/gjson"
)
// geminiToolTypes is body with a type written into each schema of its
// tools' that has none, for a Gemini model whoever serves it. Vertex AI
// takes a function declaration's parameters as its own Schema object, whose
// type is required, and turns the whole request away over one schema
// without it, though the tool is never called: 400 "Unable to submit
// request because `mcp__cua-driver__drag` functionDeclaration
// `parameters.target` schema didn't specify the schema type field."
// (kthink on Discord: dsh with cua-driver's MCP tools, Command Code's
// google/gemini-3.8-flash, which its gateway serves on Vertex). Whoever
// relays a Chat, Responses or Messages body to Vertex — a plugin's
// gateway, a router, a relay — turns the schema into Vertex's, and an
// untyped node stays untyped, so it is keyed on the model, not the
// provider.
//
// Each schema with no type is given the one its values (enum, const,
// default), keywords (properties: object, items: array, pattern:
// string …; an object's or array's before its values) or branches (anyOf, oneOf, allOf, a local $ref) say, null
// left out: Vertex has one type a node, nullable apart. A node whose
// branches have more than one type, or a $ref or union whose types can't
// be told, is left as it came: no one type is right for it. One that says
// nothing ({} or only a description) is a string, the value that carries
// anything; a tool's root is an object. "not" and "if" are left alone: a
// type there would turn the rule round. The type is written in place,
// after the schema's brace, so the rest of the body and a cache's prefix
// stay as they were; every other model's body goes as it came.
//
// Google's own Gemini API and Code Assist are sent parametersJsonSchema,
// which takes JSON Schema whole, and Antigravity its own cut-down schema
// (plainSchema), so a Gemini-shaped body isn't walked.
func geminiToolTypes(body []byte) []byte {
if !geminiNamed(gjson.GetBytes(body, "model").String()) {
return body
}
tools := gjson.GetBytes(body, "tools")
if !tools.IsArray() || tools.Index <= 0 {
return body
}
var ins []typeInsert
tools.ForEach(func(_, tool gjson.Result) bool {
for _, at := range []string{"function.parameters", "parameters", "input_schema"} {
if root := tool.Get(at); root.IsObject() {
collectGeminiUntyped(body, root, root, true, &ins)
}
}
return true
})
return withTypes(body, ins)
}
// geminiNamed is whether a model id names a Gemini model: gemini-…, or
// a relay's or router's vendor/gemini-… (google/gemini-3.8-flash,
// vertex_ai/gemini-…).
func geminiNamed(id string) bool {
if i := strings.LastIndexByte(id, '/'); i >= 0 {
id = id[i+1:]
}
return strings.HasPrefix(strings.ToLower(id), "gemini")
}
// collectGeminiUntyped adds, for node and each schema under it that has no
// type, where it opens and the type Vertex is told.
func collectGeminiUntyped(body []byte, root, node gjson.Result, isRoot bool, ins *[]typeInsert) {
switch {
case node.IsArray():
node.ForEach(func(_, v gjson.Result) bool {
collectGeminiUntyped(body, root, v, false, ins)
return true
})
case node.IsObject():
if !node.Get("type").Exists() && node.Index > 0 && node.Index < len(body) && body[node.Index] == '{' {
if t := geminiSchemaType(root, node, isRoot); t != "" {
*ins = append(*ins, typeInsert{node.Index, `"` + t + `"`})
}
}
node.ForEach(func(key, v gjson.Result) bool {
switch key.String() {
case "properties", "$defs", "definitions", "dependentSchemas", "patternProperties":
v.ForEach(func(_, s gjson.Result) bool {
collectGeminiUntyped(body, root, s, false, ins)
return true
})
case "items", "additionalProperties", "contains", "propertyNames", "then", "else", "allOf", "anyOf", "oneOf", "prefixItems", "unevaluatedProperties", "unevaluatedItems", "contentSchema":
collectGeminiUntyped(body, root, v, false, ins)
}
return true
})
}
}
// geminiSchemaType is the one type an untyped schema s is given for
// Vertex, "" when it is left as it came.
func geminiSchemaType(root, s gjson.Result, isRoot bool) string {
if t := keywordType(s); t == "object" || t == "array" {
// properties or items say it before its values do: a schema
// with items and a string enum given "string" went to Google
// with items on a string, and Antigravity turned the whole turn
// away ("properties[display].items: field predicate failed:
// $type == Type.ARRAY", ferock on Discord)
return t
}
vals := s.Get("enum").Array()
if c := s.Get("const"); c.Exists() {
vals = []gjson.Result{c}
}
if s.Get("enum").IsArray() || s.Get("const").Exists() {
var rest []gjson.Result
for _, v := range vals {
if v.Type != gjson.Null {
rest = append(rest, v)
}
}
return valuesType(rest)
}
ts := schemaTypes(root, s, 0)
delete(ts, "null")
if ts["integer"] && ts["number"] {
delete(ts, "integer")
}
if len(ts) == 1 {
for t := range ts {
return t
}
}
if len(ts) > 1 {
return ""
}
if isRoot {
return "object"
}
for _, k := range []string{`\$ref`, "anyOf", "oneOf", "allOf"} {
if s.Get(k).Exists() {
return ""
}
}
if d := s.Get("default"); d.Exists() && d.Type != gjson.Null {
switch {
case d.IsObject():
return "object"
case d.IsArray():
return "array"
}
if t := valuesType([]gjson.Result{d}); t != "" {
return t
}
}
return "string"
}