SQLModel give fastapi incomplete pydantic model if generic is used in response_model. #1668
-
First Check
Commit to Help
Example Codefrom typing import Generic, TypeVar
from fastapi import FastAPI
from sqlmodel import SQLModel
T = TypeVar("T")
app = FastAPI()
class User(SQLModel):
name: str
class UserInternal(SQLModel):
name: str
hashed_password: str
class AppResponse(SQLModel, Generic[T]):
data: T | None = None
code: int = 200
message: str = "success"
@app.get("/", response_model=AppResponse[User])
def get_user():
db_user = UserInternal(name="John Doe", hashed_password="hashed_secret")
return AppResponse(data=db_user)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8001)DescriptionReproduce
It seems that SQLModel won't give fastapi correct pydantic model if generic is used. Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.27 Python VersionPython 3.13.9 Additional Context
|
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
Dec 6, 2025
Replies: 1 comment
-
|
There is PR #1275 to fix this. I checked, your code example works well on the branch of that PR |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
TrueRou
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

There is PR #1275 to fix this.
I checked, your code example works well on the branch of that PR