File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -107,6 +107,16 @@ def _ensure_strict_json_schema(
107107 if json_schema .get ("default" , NOT_GIVEN ) is None :
108108 json_schema .pop ("default" )
109109
110+ # Remove all null values to comply with JSON Schema Draft 2020-12
111+ # This prevents LLM providers from rejecting schemas with null values
112+ keys_to_remove = []
113+ for key , value in json_schema .items ():
114+ if value is None :
115+ keys_to_remove .append (key )
116+
117+ for key in keys_to_remove :
118+ json_schema .pop (key )
119+
110120 # we can't use `$ref`s if there are also other properties defined, e.g.
111121 # `{"$ref": "...", "description": "my description"}`
112122 #
Original file line number Diff line number Diff line change @@ -124,3 +124,35 @@ def test_invalid_ref_format():
124124 schema = {"type" : "object" , "properties" : {"a" : {"$ref" : "invalid" , "description" : "desc" }}}
125125 with pytest .raises (ValueError ):
126126 ensure_strict_json_schema (schema )
127+
128+
129+ def test_null_values_removal ():
130+ """
131+ Test that null values are removed from JSON schemas to comply with JSON Schema Draft 2020-12.
132+ """
133+ schema_with_nulls = {
134+ "type" : "object" ,
135+ "properties" : {
136+ "param" : {
137+ "type" : "boolean" ,
138+ "default" : True ,
139+ "enum" : None ,
140+ "minimum" : None ,
141+ "maximum" : None ,
142+ "items" : None ,
143+ "properties" : None ,
144+ }
145+ },
146+ }
147+
148+ result = ensure_strict_json_schema (schema_with_nulls )
149+
150+ # The result should not contain any null values
151+ param_schema = result ["properties" ]["param" ]
152+ assert param_schema ["type" ] == "boolean"
153+ assert param_schema ["default" ] is True
154+ assert "enum" not in param_schema
155+ assert "minimum" not in param_schema
156+ assert "maximum" not in param_schema
157+ assert "items" not in param_schema
158+ assert "properties" not in param_schema
You can’t perform that action at this time.
0 commit comments