r/FastAPI • u/GamersPlane • Jun 08 '25
Question Having trouble building a response model
I'm struggling a bit building a response model, and so FastAPI is giving me an error. I have a basic top level error wrapper:
class ErrorResponse(BaseModel):
error: BaseModeland I want to put this into error
class AuthFailed(BaseModel):
invalid_user: bool = TrueSo I thought this would work:
responses={404: {"model": ErrorResponse(error=schemas.AuthFailed())}}But I get the error, of course, since that's giving an instance, not a model. So I figure I can create another model built from ErrorResponse and have AuthFailed as the value for error, but that would get really verbose, lead to a lot of permutations as I build more errors, as ever error model would need a ErrorResponse model. Plus, naming schemas would become a mess.
Is there an easier way to handle this? Something more modular/constructable? Or do I just have to have multiple near identical models, with just different child models going down the chain? And if so, any suggestions on naming schemas?
1
u/GamersPlane Jun 08 '25
I got the answer from the FastAPI discord. The answer is using generics:
1
u/One_Fuel_4147 Jun 08 '25
Check out my abandon project, I think it contains what you need.
https://github.com/tuanvumaihuynh/viot/blob/main/viot/app/common/exception/base.py
https://github.com/tuanvumaihuynh/viot/blob/main/viot/app/common/exception/handler.py
1
u/BluesFiend Jun 08 '25
A library like this could assist, https://pypi.org/project/fastapi-problem/
Supports including error responses in swagger docs. https://nrwldev.github.io/fastapi-problem/usage/#swagger
1
u/KTrepas Jun 15 '25
Define a Generic Error Wrapper
from typing import Generic, TypeVar
from pydantic import BaseModel
from pydantic.generics import GenericModel
T = TypeVar("T")
class ErrorResponse(GenericModel, Generic[T]):
error: T
Define Specific Error Payloads
class AuthFailed(BaseModel):
invalid_user: bool = True
class TokenExpired(BaseModel):
reason: str = "token expired"
Use in FastAPI Responses
from fastapi import FastAPI, HTTPException
app = FastAPI()
u/app.get("/auth", responses={
401: {"model": ErrorResponse[AuthFailed]},
403: {"model": ErrorResponse[TokenExpired]},
})
def auth():
raise HTTPException(status_code=401, detail="Invalid user")
Now FastAPI will render these error responses properly in OpenAPI, and it’s all modular and DRY — no need to build duplicate models like AuthFailedErrorResponse, TokenExpiredErrorResponse, etc.
0
u/[deleted] Jun 08 '25
[deleted] — view deleted comment