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
pydantic.errors.PydanticUserError: typing.Self is invalid in this context
I expected FastAPI to generate the OpenAPI schema and allow me to see the recursive model structure in the docs. How can I define recursive Pydantic models in FastAPI so that OpenAPI generation works correctly with Self or similar constructs?
Operating System
Linux, Windows
Operating System Details
No response
FastAPI Version
FastAPI Version: 0.121.1
Pydantic Version
Pydantic Version: 2.12.4
Python Version
Python Version: 3.14.0
Additional Context
I want to use recursive models for tree-like data structures (e.g., nested menus, organizational charts) and have proper OpenAPI documentation generated automatically.
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
pydantic.errors.PydanticUserError: typing.Self is invalid in this context
I expected FastAPI to generate the OpenAPI schema and allow me to see the recursive model structure in the docs. How can I define recursive Pydantic models in FastAPI so that OpenAPI generation works correctly with Self or similar constructs?
Operating System
Linux, Windows
Operating System Details
No response
FastAPI Version
FastAPI Version: 0.121.1
Pydantic Version
Pydantic Version: 2.12.4
Python Version
Python Version: 3.14.0
Additional Context
I want to use recursive models for tree-like data structures (e.g., nested menus, organizational charts) and have proper OpenAPI documentation generated automatically.
Beta Was this translation helpful? Give feedback.
All reactions