gateway: xAI's null-required walk goes through a schema's arrays too (#3392, kongkongklng)
The fourth round of #3392: after the null-required fix the reporter's claude calls still 400ed the same '/required: null is not of type "array"'. dropNullRequired walked a schema's objects but not its arrays — a null under a union's anyOf branch or a tuple items survived the shaping and reached xAI as before. The walk now takes arrays too; a real array of names stays as it came. Run on mote. At this head, rebased on origin/main6bec526f7: go test -tags nogui -count=1 -timeout 45m ./internal/gateway ./internal/agent ok (661s, 337s); gofmt clean; go vet -tags nogui ./internal/gateway ./internal/agent clean, and ./internal/gateway on linux and windows clean. Pushed on origin/main9e33962c0, whose four newer commits touch neither package (GUI, notices, settings). Before the first rebase, ona57c95513: -run "Custom|XAI|Xai|Grok|Compact" ok and the whole gateway package ok (546s). With the regex addition taken out and xaiBody returning early, TestCustomToolRefusalWordings, TestXAITakesCodexsSealedReasoningBack, TestXAILearnsCustomToolsThenTakesTheReplay, TestXAIFoldsClaudesSystemTurn and TestXAITakesANullRequiredToolSchema go red. Not tried: the real xAI API (no key here). From skygunner's branch fix/issue-batch-1024pm; kongkongklng confirmed it on 10-25.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user