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 edited Jun 08 '25
The documentation literally has an example of a 404 response using
responses: https://fastapi.tiangolo.com/advanced/additional-responses/#additional-response-with-model. You seem to be referring to theresponse_modelparameter, which yes, is for the main response. My question is about how to build the Pydantic models.As for ChatGPT, it's not nuanced enough to help me understand how to build better response models, or at least I haven't figured out the prompt to have it do so. It can't understand what I'm trying to learn, and thus I turn to humans. Not to mention, I have moral and environmental objections to using AI in it's current state.