@@ -207,10 +207,13 @@ def is_pydantic_model(obj):
207207def get_nested_fields (model : Type [BaseModel ]):
208208 nested_fields = {}
209209 for field_name , field in model .__fields__ .items ():
210- if is_pydantic_model (field .type_ ):
211- nested_fields [field_name ] = field .type_
212- elif get_origin (field .type_ ) is List and is_pydantic_model (get_args (field .type_ )[0 ]):
213- nested_fields [field_name ] = get_args (field .type_ )[0 ]
210+ # Handle both Pydantic v1 and v2 field access
211+ field_type = getattr (field , "type_" , getattr (field , "annotation" , None ))
212+
213+ if is_pydantic_model (field_type ):
214+ nested_fields [field_name ] = field_type
215+ elif get_origin (field_type ) is List and is_pydantic_model (get_args (field_type )[0 ]):
216+ nested_fields [field_name ] = get_args (field_type )[0 ]
214217 return nested_fields
215218
216219
@@ -241,7 +244,10 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
241244 match_conditions [full_mongo_field_name ] = {"$exists" : True }
242245
243246 if field_name in nested_fields :
244- if get_origin (field_type .type_ ) is List :
247+ # Handle both Pydantic v1 and v2 field access
248+ field_annotation = getattr (field_type , "type_" , getattr (field_type , "annotation" , None ))
249+
250+ if get_origin (field_annotation ) is List :
245251 nested_pipeline , nested_match = create_nested_pipeline (
246252 nested_fields [field_name ], "" # Empty prefix for list items
247253 )
0 commit comments