Skip to content

Commit 7e17bc5

Browse files
authored
fix(go/plugins/googlegenai): parse anyOf in schema (#3674)
1 parent 48a2941 commit 7e17bc5

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

go/plugins/googlegenai/gemini.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,48 @@ func toGeminiSchema(originalSchema map[string]any, genkitSchema map[string]any)
480480
return nil, nil
481481
}
482482
if v, ok := genkitSchema["$ref"]; ok {
483-
ref := v.(string)
483+
ref, ok := v.(string)
484+
if !ok {
485+
return nil, fmt.Errorf("invalid $ref value: not a string")
486+
}
484487
return toGeminiSchema(originalSchema, resolveRef(originalSchema, ref))
485488
}
489+
490+
// Handle "anyOf" subschemas by finding the first valid schema definition
491+
if v, ok := genkitSchema["anyOf"]; ok {
492+
if anyOfList, isList := v.([]map[string]any); isList {
493+
for _, subSchema := range anyOfList {
494+
if subSchemaType, hasType := subSchema["type"]; hasType {
495+
if typeStr, isString := subSchemaType.(string); isString && typeStr != "null" {
496+
if title, ok := genkitSchema["title"]; ok {
497+
subSchema["title"] = title
498+
}
499+
if description, ok := genkitSchema["description"]; ok {
500+
subSchema["description"] = description
501+
}
502+
// Found a schema like: {"type": "string"}
503+
return toGeminiSchema(originalSchema, subSchema)
504+
}
505+
}
506+
}
507+
}
508+
}
509+
486510
schema := &genai.Schema{}
511+
typeVal, ok := genkitSchema["type"]
512+
if !ok {
513+
return nil, fmt.Errorf("schema is missing the 'type' field: %#v", genkitSchema)
514+
}
515+
516+
typeStr, ok := typeVal.(string)
517+
if !ok {
518+
return nil, fmt.Errorf("schema 'type' field is not a string, but %T", typeVal)
519+
}
487520

488-
switch genkitSchema["type"].(string) {
521+
switch typeStr {
489522
case "string":
490523
schema.Type = genai.TypeString
491-
case "float64":
492-
schema.Type = genai.TypeNumber
493-
case "number":
524+
case "float64", "number":
494525
schema.Type = genai.TypeNumber
495526
case "integer":
496527
schema.Type = genai.TypeInteger

go/plugins/googlegenai/gemini_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ func TestConvertRequest(t *testing.T) {
7878
"object": map[string]any{
7979
"type": string("object"),
8080
},
81+
"domain": map[string]any{
82+
"anyOf": []map[string]any{
83+
{
84+
"type": string("string"),
85+
},
86+
{
87+
"type": string("null"),
88+
},
89+
},
90+
"default": "null",
91+
"title": string("Domain"),
92+
},
8193
},
8294
},
8395
},

0 commit comments

Comments
 (0)