@@ -1684,39 +1684,57 @@ def _generate_detailed_schema(self, model: Type[BaseModel], depth: int = 0) -> s
16841684 fields = get_type_hints (model )
16851685 field_descriptions = []
16861686 indent = " " * depth
1687-
16881687 for field , field_type in fields .items ():
16891688 description = f"{ indent } { field } : "
1690-
1689+ print ( f"Processing field: { field } , type: { field_type } " ) # Debug print
16911690 origin_type = get_origin (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
1691+ if origin_type is None :
1692+ origin_type = field_type
1693+ print (f"Origin type: { origin_type } " ) # Debug print
1694+ if inspect .isclass (origin_type ) and issubclass (origin_type , BaseModel ):
1695+ description += f"Nested Model:\n { self ._generate_detailed_schema (origin_type , depth + 1 )} "
1696+ elif origin_type == list :
1697+ list_type = get_args (field_type )[0 ]
1698+ print (f"List type: { list_type } " ) # Debug print
1699+ if inspect .isclass (list_type ) and issubclass (list_type , BaseModel ):
1700+ description += f"List of Nested Model:\n { self ._generate_detailed_schema (list_type , depth + 1 )} "
1701+ elif get_origin (list_type ) == Union :
1702+ union_types = get_args (list_type )
1703+ description += f"List of Union:\n "
1704+ for union_type in union_types :
1705+ if inspect .isclass (union_type ) and issubclass (
1706+ union_type , BaseModel
1707+ ):
1708+ description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
1709+ else :
1710+ description += (
1711+ f"{ indent } - { self ._get_type_name (union_type )} \n "
1712+ )
1713+ else :
1714+ description += f"List[{ self ._get_type_name (list_type )} ]"
1715+ elif origin_type == dict :
1716+ key_type , value_type = get_args (field_type )
17031717 description += f"Dict[{ self ._get_type_name (key_type )} , { self ._get_type_name (value_type )} ]"
1704- else :
1705- description += self . _process_type (field_type , depth )
1718+ elif origin_type == Union :
1719+ union_types = get_args (field_type )
17061720
1721+ for union_type in union_types :
1722+ if inspect .isclass (union_type ) and issubclass (
1723+ union_type , BaseModel
1724+ ):
1725+ description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
1726+ else :
1727+ type_name = self ._get_type_name (union_type )
1728+ if type_name != "NoneType" :
1729+ description += f"{ self ._get_type_name (union_type )} \n "
1730+ elif inspect .isclass (origin_type ) and issubclass (origin_type , Enum ):
1731+ enum_values = ", " .join ([f"{ e .name } = { e .value } " for e in origin_type ])
1732+ description += f"{ origin_type .__name__ } (Enum values: { enum_values } )"
1733+ else :
1734+ description += self ._get_type_name (origin_type )
17071735 field_descriptions .append (description )
1708-
17091736 return "\n " .join (field_descriptions )
17101737
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-
17201738 def _get_type_name (self , type_ ):
17211739 """Helper method to get the name of a type, handling some special cases."""
17221740 if hasattr (type_ , "__name__" ):
0 commit comments