11"""
2- This utility function trasfrom the pydantic schema into a more comprehensible schema.
2+ This utility function transforms the pydantic schema into a more comprehensible schema.
33"""
44
55
@@ -19,25 +19,35 @@ def process_properties(properties):
1919 for key , value in properties .items ():
2020 if "type" in value :
2121 if value ["type" ] == "array" :
22- if "$ref" in value ["items" ]:
22+ if "items" in value and " $ref" in value ["items" ]:
2323 ref_key = value ["items" ]["$ref" ].split ("/" )[- 1 ]
24- result [key ] = [
25- process_properties (
26- pydantic_schema ["$defs" ][ref_key ]["properties" ]
27- )
28- ]
29- else :
24+ if "$defs" in pydantic_schema and ref_key in pydantic_schema ["$defs" ]:
25+ result [key ] = [
26+ process_properties (
27+ pydantic_schema ["$defs" ][ref_key ].get ("properties" , {})
28+ )
29+ ]
30+ else :
31+ result [key ] = ["object" ] # fallback for missing reference
32+ elif "items" in value and "type" in value ["items" ]:
3033 result [key ] = [value ["items" ]["type" ]]
34+ else :
35+ result [key ] = ["unknown" ] # fallback for malformed array
3136 else :
3237 result [key ] = {
3338 "type" : value ["type" ],
3439 "description" : value .get ("description" , "" ),
3540 }
3641 elif "$ref" in value :
3742 ref_key = value ["$ref" ].split ("/" )[- 1 ]
38- result [key ] = process_properties (
39- pydantic_schema ["$defs" ][ref_key ]["properties" ]
40- )
43+ if "$defs" in pydantic_schema and ref_key in pydantic_schema ["$defs" ]:
44+ result [key ] = process_properties (
45+ pydantic_schema ["$defs" ][ref_key ].get ("properties" , {})
46+ )
47+ else :
48+ result [key ] = {"type" : "object" , "description" : "Missing reference" } # fallback
4149 return result
4250
51+ if "properties" not in pydantic_schema :
52+ raise ValueError ("Invalid pydantic schema: missing 'properties' key" )
4353 return process_properties (pydantic_schema ["properties" ])
0 commit comments