2121 Union ,
2222)
2323from enum import Enum
24- from pydantic import BaseModel
24+ import inspect
25+ from typing import get_type_hints
2526import json
2627
2728
@@ -1680,47 +1681,58 @@ def _generate_detailed_schema(self, model: Type[BaseModel], depth: int = 0) -> s
16801681 Recursively generates a detailed schema representation of a Pydantic model,
16811682 including nested models and complex types.
16821683 """
1683- fields = model . __annotations__
1684+ fields = get_type_hints ( model )
16841685 field_descriptions = []
16851686 indent = " " * depth
16861687
16871688 for field , field_type in fields .items ():
16881689 description = f"{ indent } { field } : "
16891690
1691+ print (f"Processing field: { field } , type: { field_type } " ) # Debug print
1692+
16901693 origin_type = get_origin (field_type )
16911694 if origin_type is None :
16921695 origin_type = field_type
16931696
1694- if issubclass (origin_type , BaseModel ):
1697+ print (f"Origin type: { origin_type } " ) # Debug print
1698+
1699+ if inspect .isclass (origin_type ) and issubclass (origin_type , BaseModel ):
16951700 description += f"Nested Model:\n { self ._generate_detailed_schema (origin_type , depth + 1 )} "
1696- elif origin_type == List :
1701+ elif origin_type == list :
16971702 list_type = get_args (field_type )[0 ]
1698- if isinstance (list_type , type ) and issubclass (list_type , BaseModel ):
1703+ print (f"List type: { list_type } " ) # Debug print
1704+ if inspect .isclass (list_type ) and issubclass (list_type , BaseModel ):
16991705 description += f"List of Nested Model:\n { self ._generate_detailed_schema (list_type , depth + 1 )} "
17001706 elif get_origin (list_type ) == Union :
17011707 union_types = get_args (list_type )
17021708 description += f"List of Union:\n "
17031709 for union_type in union_types :
1704- if issubclass (union_type , BaseModel ):
1710+ if inspect .isclass (union_type ) and issubclass (
1711+ union_type , BaseModel
1712+ ):
17051713 description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
17061714 else :
1707- description += f"{ indent } - { union_type .__name__ } \n "
1715+ description += (
1716+ f"{ indent } - { self ._get_type_name (union_type )} \n "
1717+ )
17081718 else :
17091719 description += f"List[{ self ._get_type_name (list_type )} ]"
1710- elif origin_type == Dict :
1720+ elif origin_type == dict :
17111721 key_type , value_type = get_args (field_type )
17121722 description += f"Dict[{ self ._get_type_name (key_type )} , { self ._get_type_name (value_type )} ]"
17131723 elif origin_type == Union :
17141724 union_types = get_args (field_type )
17151725 description += "Union of:\n "
17161726 for union_type in union_types :
1717- if issubclass (union_type , BaseModel ):
1727+ if inspect .isclass (union_type ) and issubclass (
1728+ union_type , BaseModel
1729+ ):
17181730 description += f"{ indent } - Nested Model:\n { self ._generate_detailed_schema (union_type , depth + 2 )} "
17191731 else :
17201732 description += (
17211733 f"{ indent } - { self ._get_type_name (union_type )} \n "
17221734 )
1723- elif issubclass (origin_type , Enum ):
1735+ elif inspect . isclass ( origin_type ) and issubclass (origin_type , Enum ):
17241736 enum_values = ", " .join ([f"{ e .name } = { e .value } " for e in origin_type ])
17251737 description += f"{ origin_type .__name__ } (Enum values: { enum_values } )"
17261738 else :
@@ -1758,12 +1770,10 @@ def convert_to_model(
17581770 """
17591771 input_string = str (input_string )
17601772 schema = self ._generate_detailed_schema (model )
1761-
17621773 if "user_input" in kwargs :
17631774 del kwargs ["user_input" ]
17641775 if "schema" in kwargs :
17651776 del kwargs ["schema" ]
1766-
17671777 response = self .prompt_agent (
17681778 agent_name = agent_name ,
17691779 prompt_name = "Convert to Model" ,
@@ -1773,12 +1783,10 @@ def convert_to_model(
17731783 ** kwargs ,
17741784 },
17751785 )
1776-
17771786 if "```json" in response :
17781787 response = response .split ("```json" )[1 ].split ("```" )[0 ].strip ()
17791788 elif "```" in response :
17801789 response = response .split ("```" )[1 ].strip ()
1781-
17821790 try :
17831791 response = json .loads (response )
17841792 if response_type == "json" :
@@ -1792,7 +1800,11 @@ def convert_to_model(
17921800 f"Error: { e } . Failed to convert the response to the model after { max_failures } attempts. Response: { response } "
17931801 )
17941802 self .failures = 0
1795- return response if response else "Failed to convert the response to the model."
1803+ return (
1804+ response
1805+ if response
1806+ else "Failed to convert the response to the model."
1807+ )
17961808 else :
17971809 self .failures = 1
17981810 print (
0 commit comments