fix: enhance nullable function parameter handling and add tests for local and external references

This commit is contained in:
Chenyme committed 2026-07-24 16:26:33 +08:00
1 parent bbd7e3806a
commit 35f5a3a482
2 files changed
+98 -5

No files matched your search

@@ -326,7 +326,7 @@ func normalizeBuildFunctionParametersRoot(value any, param string) (any, bool, e
}
if len(filtered) == 1 {
branch, ok := filtered[0].(map[string]any)
if !ok || !isObjectRootSchema(branch) {
if !ok || !isObjectRootSchema(branch, normalized, nil) {
return nil, false, invalidBuildFunctionParametersRoot(param)
}
if len(normalized) == 1 {
@@ -340,7 +340,7 @@ func normalizeBuildFunctionParametersRoot(value any, param string) (any, bool, e
}
for _, rawBranch := range filtered {
branch, ok := rawBranch.(map[string]any)
if !ok || !isObjectRootSchema(branch) {
if !ok || !isObjectRootSchema(branch, normalized, nil) {
return nil, false, invalidBuildFunctionParametersRoot(param)
}
}
@@ -384,7 +384,7 @@ func isNullOnlySchema(value any) bool {
return true
}
func isObjectRootSchema(schema map[string]any) bool {
func isObjectRootSchema(schema, root map[string]any, visited map[string]struct{}) bool {
rawType, hasType := schema["type"]
if rawType == "object" {
return true
@@ -400,8 +400,48 @@ func isObjectRootSchema(schema map[string]any) bool {
if hasType {
return false
}
_, hasProperties := schema["properties"]
return hasProperties
if _, hasProperties := schema["properties"]; hasProperties {
return true
}
ref, ok := schema["$ref"].(string)
if !ok {
return false
}
if visited == nil {
visited = make(map[string]struct{})
}
if _, seen := visited[ref]; seen {
return false
}
resolved, ok := resolveLocalSchemaRef(root, ref)
if !ok {
return false
}
visited[ref] = struct{}{}
return isObjectRootSchema(resolved, root, visited)
}
func resolveLocalSchemaRef(root map[string]any, ref string) (map[string]any, bool) {
if ref == "#" {
return root, true
}
if !strings.HasPrefix(ref, "#/") {
return nil, false
}
var current any = root
for _, encoded := range strings.Split(strings.TrimPrefix(ref, "#/"), "/") {
segment := strings.ReplaceAll(strings.ReplaceAll(encoded, "~1", "/"), "~0", "~")
object, ok := current.(map[string]any)
if !ok {
return nil, false
}
current, ok = object[segment]
if !ok {
return nil, false
}
}
resolved, ok := current.(map[string]any)
return resolved, ok
}
func invalidBuildFunctionParametersRoot(param string) error {
@@ -313,6 +313,59 @@ func TestNormalizeResponsesRequestRejectsNullableNonObjectFunctionRoot(t *testin
}
}
func TestNormalizeResponsesRequestRemovesNullableLocalRefFunctionRoot(t *testing.T) {
normalized, compatibility, err := normalizeResponsesRequest([]byte(`{
"model":"public","input":"hello",
"tools":[{"type":"function","name":"lookup","parameters":{
"$defs":{"Args":{
"type":"object",
"properties":{"query":{"type":"string"}},
"required":["query"]
}},
"anyOf":[{"$ref":"#/$defs/Args"},{"type":"null"}]
}}]
}`), "grok-4.5")
if err != nil {
t.Fatal(err)
}
if compatibility == nil || !strings.Contains(compatibility.warningHeader(), "function_parameters_nullable_root_normalized") {
t.Fatalf("compatibility = %#v", compatibility)
}
var payload map[string]any
if err := json.Unmarshal(normalized, &payload); err != nil {
t.Fatal(err)
}
parameters := payload["tools"].([]any)[0].(map[string]any)["parameters"].(map[string]any)
if parameters["type"] != "object" {
t.Fatalf("upstream parameters = %#v", parameters)
}
branches := parameters["anyOf"].([]any)
if len(branches) != 1 || branches[0].(map[string]any)["$ref"] != "#/$defs/Args" {
t.Fatalf("upstream branches = %#v", branches)
}
visibleParameters := compatibility.visibleTools[0].(map[string]any)["parameters"].(map[string]any)
visibleBranches := visibleParameters["anyOf"].([]any)
if len(visibleBranches) != 2 || visibleParameters["type"] != nil {
t.Fatalf("visible parameters were mutated = %#v", visibleParameters)
}
}
func TestNormalizeResponsesRequestRejectsNullableExternalRefFunctionRoot(t *testing.T) {
_, _, err := normalizeResponsesRequest([]byte(`{
"model":"public","input":"hello",
"tools":[{"type":"function","name":"lookup","parameters":{
"anyOf":[
{"$ref":"https://example.com/schema.json#/$defs/Args"},
{"type":"null"}
]
}}]
}`), "grok-4.5")
requestErr, ok := err.(*responsesRequestError)
if !ok || requestErr.Param != "tools[0].parameters" || requestErr.Code != "invalid_parameter" {
t.Fatalf("error = %#v", err)
}
}
func TestNormalizeBuildFunctionParametersRootVariants(t *testing.T) {
tests := []struct {
name string