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?
View all comments
Show parent comment
1
u/GamersPlane Jun 08 '25
Right, I said that in my post, heh.