@@ -35,13 +35,18 @@ def _clean_schema_for_google(self, schema: Dict[str, Any]) -> Dict[str, Any]:
35
35
"""
36
36
Recursively removes unsupported JSON schema keywords for google.genai.types.Schema.
37
37
Specifically removes 'additionalProperties', '$schema', 'exclusiveMaximum', and 'exclusiveMinimum'.
38
+ Also resolves $ref references and inlines $defs.
38
39
"""
40
+ # First, resolve any $ref references in the schema
41
+ schema = self ._resolve_refs (schema , schema )
42
+
39
43
cleaned_schema = {}
40
44
unsupported_keys = {
41
45
"additionalProperties" ,
42
46
"$schema" ,
43
47
"exclusiveMaximum" ,
44
48
"exclusiveMinimum" ,
49
+ "$defs" , # Remove $defs after resolving references
45
50
}
46
51
supported_string_formats = {"enum" , "date-time" }
47
52
@@ -66,6 +71,54 @@ def _clean_schema_for_google(self, schema: Dict[str, Any]) -> Dict[str, Any]:
66
71
else :
67
72
cleaned_schema [key ] = value
68
73
return cleaned_schema
74
+
75
+ def _resolve_refs (self , schema : Dict [str , Any ], root_schema : Dict [str , Any ]) -> Dict [str , Any ]:
76
+ """
77
+ Resolve $ref references in a JSON schema by inlining the referenced definitions.
78
+
79
+ Args:
80
+ schema: The current schema fragment being processed
81
+ root_schema: The root schema containing $defs
82
+
83
+ Returns:
84
+ Schema with $ref references resolved
85
+ """
86
+ if not isinstance (schema , dict ):
87
+ return schema
88
+
89
+ # If this is a $ref, resolve it
90
+ if "$ref" in schema :
91
+ ref_path = schema ["$ref" ]
92
+ if ref_path .startswith ("#/" ):
93
+ # Parse the reference path (e.g., "#/$defs/HumanInputRequest")
94
+ path_parts = ref_path [2 :].split ("/" ) # Remove "#/" and split
95
+
96
+ # Navigate to the referenced definition
97
+ ref_target = root_schema
98
+ for part in path_parts :
99
+ if part in ref_target :
100
+ ref_target = ref_target [part ]
101
+ else :
102
+ # If reference not found, return the original schema
103
+ return schema
104
+
105
+ # Return the resolved definition (recursively resolve any nested refs)
106
+ return self ._resolve_refs (ref_target , root_schema )
107
+
108
+ # Otherwise, recursively process all values in the schema
109
+ resolved = {}
110
+ for key , value in schema .items ():
111
+ if isinstance (value , dict ):
112
+ resolved [key ] = self ._resolve_refs (value , root_schema )
113
+ elif isinstance (value , list ):
114
+ resolved [key ] = [
115
+ self ._resolve_refs (item , root_schema ) if isinstance (item , dict ) else item
116
+ for item in value
117
+ ]
118
+ else :
119
+ resolved [key ] = value
120
+
121
+ return resolved
69
122
70
123
def convert_to_google_content (
71
124
self , messages : List [PromptMessageMultipart ]
0 commit comments