@@ -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
0 commit comments