You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m trying to use a recursive Pydantic model with optional children. The model works when I return it directly from the endpoint, but the generated OpenAPI schema is invalid.
Swagger UI fails to render the model correctly.
ReDoc shows null for children instead of a list of objects.
I expected OpenAPI to correctly represent a recursive array structure where children can be empty or null.
Operating System
macOS
Operating System Details
No response
FastAPI Version
FastAPI Version: 0.121.x
Pydantic Version
Pydantic Version: 2.x
Python Version
Python Version: 3.14
Additional Context
I’ve tried update_forward_refs() and using typing.Self instead of string literals for recursion, but the OpenAPI spec still doesn’t generate correctly. Is there a recommended pattern for recursive models with optional children in FastAPI v0.121 + Pydantic v2?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I’m trying to use a recursive Pydantic model with optional children. The model works when I return it directly from the endpoint, but the generated OpenAPI schema is invalid.
Swagger UI fails to render the model correctly.
ReDoc shows null for children instead of a list of objects.
I expected OpenAPI to correctly represent a recursive array structure where children can be empty or null.
Operating System
macOS
Operating System Details
No response
FastAPI Version
FastAPI Version: 0.121.x
Pydantic Version
Pydantic Version: 2.x
Python Version
Python Version: 3.14
Additional Context
I’ve tried update_forward_refs() and using typing.Self instead of string literals for recursion, but the OpenAPI spec still doesn’t generate correctly. Is there a recommended pattern for recursive models with optional children in FastAPI v0.121 + Pydantic v2?
Beta Was this translation helpful? Give feedback.
All reactions