FastAPI schema generation fails for models (SQLModel) with forward references #726
Replies: 3 comments 1 reply
-
I can reproduce this error. Windows 10 I have the following additional example. Here, an error-free reordering does not seem to be possible. This is probably because regardless of ordering, one of the types must be a "forward reference" because of the cyclic type referencing.
Result: If I change the type alias to a
Unfortunately, |
Beta Was this translation helpful? Give feedback.
-
Just checked it on last versions (FastAPI 0.109.2, Pydantic 2.6.1, SQLModel 0.0.16), it works well and doesn't fail. |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar issue with my models split across different files. It seems like Pydantic can't handle not having access to the full class. I found an ugly workaround: see here |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
FastAPI app fails (
pydantic.errors.PydanticUndefinedAnnotation: name 'HeroRead' is not defined
) on startup (before I use any endpoints) with this code.But if I move
TeamReadWithHeroes
definition below theHeroRead
definition then it works well.I wouldn't like to move it, because I want to group my models to make my code more readable.
With Pydantic models it works well in either case.
Traceback:
Operating System
Linux
Operating System Details
Ubuntu 20.04.6 LTS
SQLModel Version
0.0.14
Python Version
3.10.13
Additional Context
FastAPI version: 0.104.1
Pydantic version: 2.5.2
Beta Was this translation helpful? Give feedback.
All reactions