@@ -207,10 +207,13 @@ def is_pydantic_model(obj):
207
207
def get_nested_fields (model : Type [BaseModel ]):
208
208
nested_fields = {}
209
209
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 ]
214
217
return nested_fields
215
218
216
219
@@ -241,7 +244,10 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
241
244
match_conditions [full_mongo_field_name ] = {"$exists" : True }
242
245
243
246
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 :
245
251
nested_pipeline , nested_match = create_nested_pipeline (
246
252
nested_fields [field_name ], "" # Empty prefix for list items
247
253
)
0 commit comments