@@ -1688,60 +1688,35 @@ def _generate_detailed_schema(self, model: Type[BaseModel], depth: int = 0) -> s
16881688 for field , field_type in fields .items ():
16891689 description = f"{ indent } { field } : "
16901690
1691- print (f"Processing field: { field } , type: { field_type } " ) # Debug print
1692-
16931691 origin_type = get_origin (field_type )
1694- if origin_type is None :
1695- origin_type = field_type
1696-
1697- print (f"Origin type: { origin_type } " ) # Debug print
1698-
1699- if inspect .isclass (origin_type ) and issubclass (origin_type , BaseModel ):
1700- description += f"Nested Model:\n { self ._generate_detailed_schema (origin_type , depth + 1 )} "
1701- elif origin_type == list :
1702- list_type = get_args (field_type )[0 ]
1703- print (f"List type: { list_type } " ) # Debug print
1704- if inspect .isclass (list_type ) and issubclass (list_type , BaseModel ):
1705- description += f"List of Nested Model:\n { self ._generate_detailed_schema (list_type , depth + 1 )} "
1706- elif get_origin (list_type ) == Union :
1707- union_types = get_args (list_type )
1708- description += f"List of Union:\n "
1709- for union_type in union_types :
1710- if inspect .isclass (union_type ) and issubclass (
1711- union_type , BaseModel
1712- ):
1713- description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
1714- else :
1715- description += (
1716- f"{ indent } - { self ._get_type_name (union_type )} \n "
1717- )
1718- else :
1719- description += f"List[{ self ._get_type_name (list_type )} ]"
1720- elif origin_type == dict :
1721- key_type , value_type = get_args (field_type )
1692+ args = get_args (field_type )
1693+
1694+ if origin_type is Union and type (None ) in args :
1695+ # This is an Optional type
1696+ non_none_type = next (arg for arg in args if arg is not type (None ))
1697+ description += f"Optional[{ self ._process_type (non_none_type , depth )} ]"
1698+ elif origin_type is List :
1699+ item_type = args [0 ]
1700+ description += f"List[{ self ._process_type (item_type , depth )} ]"
1701+ elif origin_type is Dict :
1702+ key_type , value_type = args
17221703 description += f"Dict[{ self ._get_type_name (key_type )} , { self ._get_type_name (value_type )} ]"
1723- elif origin_type == Union :
1724- union_types = get_args (field_type )
1725- description += "Union of:\n "
1726- for union_type in union_types :
1727- if inspect .isclass (union_type ) and issubclass (
1728- union_type , BaseModel
1729- ):
1730- description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
1731- else :
1732- description += (
1733- f"{ indent } - { self ._get_type_name (union_type )} \n "
1734- )
1735- elif inspect .isclass (origin_type ) and issubclass (origin_type , Enum ):
1736- enum_values = ", " .join ([f"{ e .name } = { e .value } " for e in origin_type ])
1737- description += f"{ origin_type .__name__ } (Enum values: { enum_values } )"
17381704 else :
1739- description += self ._get_type_name ( origin_type )
1705+ description += self ._process_type ( field_type , depth )
17401706
17411707 field_descriptions .append (description )
17421708
17431709 return "\n " .join (field_descriptions )
17441710
1711+ def _process_type (self , type_ , depth ):
1712+ if inspect .isclass (type_ ) and issubclass (type_ , BaseModel ):
1713+ return f"Nested Model:\n { self ._generate_detailed_schema (type_ , depth + 1 )} "
1714+ elif inspect .isclass (type_ ) and issubclass (type_ , Enum ):
1715+ enum_values = ", " .join ([f"{ e .name } = { e .value } " for e in type_ ])
1716+ return f"{ type_ .__name__ } (Enum values: { enum_values } )"
1717+ else :
1718+ return self ._get_type_name (type_ )
1719+
17451720 def _get_type_name (self , type_ ):
17461721 """Helper method to get the name of a type, handling some special cases."""
17471722 if hasattr (type_ , "__name__" ):
0 commit comments