diff --git a/internal/gateway/xai_body.go b/internal/gateway/xai_body.go index d8bef6fc0..c1dacf13e 100644 --- a/internal/gateway/xai_body.go +++ b/internal/gateway/xai_body.go @@ -190,22 +190,32 @@ func withoutNullToolRequired(body []byte) []byte { return out } -// dropNullRequired deletes every "required": null in v, recursively, and -// says whether it did; a real array of names stays as it came. +// dropNullRequired deletes every "required": null in v, recursively — +// through a schema's objects and its arrays alike, where a union's anyOf +// branch or a tuple items holds one — and says whether it did; a real +// array of names stays as it came. func dropNullRequired(v any) bool { - obj, _ := v.(map[string]any) - if obj == nil { - return false - } - dirty := false - if r, there := obj["required"]; there && r == nil { - delete(obj, "required") - dirty = true - } - for _, child := range obj { - if dropNullRequired(child) { + switch t := v.(type) { + case map[string]any: + dirty := false + if r, there := t["required"]; there && r == nil { + delete(t, "required") dirty = true } + for _, child := range t { + if dropNullRequired(child) { + dirty = true + } + } + return dirty + case []any: + dirty := false + for _, child := range t { + if dropNullRequired(child) { + dirty = true + } + } + return dirty } - return dirty + return false } diff --git a/internal/gateway/xai_body_test.go b/internal/gateway/xai_body_test.go index d429b1946..d2786fcde 100644 --- a/internal/gateway/xai_body_test.go +++ b/internal/gateway/xai_body_test.go @@ -234,12 +234,15 @@ func TestXAITakesANullRequiredToolSchema(t *testing.T) { }}} // the reporter's shape: Bash's schema with its real required array // beside an MCP-flavoured tool whose schema says required: null at the - // root and one whose nested property does (texts cut) + // root, one whose nested property does, and two whose null sits under + // an array — a zod union's anyOf branch and a tuple items (texts cut) rec := httptest.NewRecorder() s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"fake/grok-4.7","stream":true,"max_tokens":128000, "tools":[{"name":"Bash","description":"Run a command","input_schema":{"type":"object","properties":{"command":{"type":"string"}},"required":["command"]}}, {"name":"mcp__server__search","description":"Search","input_schema":{"type":"object","properties":{"query":{"type":"string"}},"required":null}}, - {"name":"mcp__server__nested","description":"Nested","input_schema":{"type":"object","properties":{"opts":{"type":"object","properties":{"a":{"type":"string"}},"required":null},"b":{"type":"number"}}}}], + {"name":"mcp__server__nested","description":"Nested","input_schema":{"type":"object","properties":{"opts":{"type":"object","properties":{"a":{"type":"string"}},"required":null},"b":{"type":"number"}}}}, + {"name":"mcp__server__union","description":"Union","input_schema":{"type":"object","properties":{"filter":{"anyOf":[{"type":"object","properties":{"id":{"type":"string"}},"required":null},{"type":"string"}]}}}}, + {"name":"mcp__server__pair","description":"Pair","input_schema":{"type":"object","items":[{"type":"object","properties":{"first":{"type":"string"}},"required":null}],"type":"object"}}], "messages":[{"role":"user","content":[{"type":"text","text":"hello"}]}]}`))) if rec.Code != 200 { t.Fatalf("%d %s\nxAI got %s", rec.Code, rec.Body, f.got) @@ -247,7 +250,7 @@ func TestXAITakesANullRequiredToolSchema(t *testing.T) { if bytes.Contains(f.got, []byte(`"required":null`)) { t.Errorf("null required left in a tool schema: %s", f.got) } - for _, said := range []string{`"required":["command"]`, `"query"`, `"opts"`, `"mcp__server__nested"`} { + for _, said := range []string{`"required":["command"]`, `"query"`, `"opts"`, `"mcp__server__nested"`, `"anyOf"`, `"first"`} { if !bytes.Contains(f.got, []byte(said)) { t.Errorf("xAI's schemas lost %q: %s", said, f.got) }