[BUG] Unable to have List of Pydantic Model as route 'responses' model
See original GitHub issueDescribe the bug
When using responses={...: {"model": List[PydanticModel]}}
in a route function there is an exception returned AssertionError: A response model must be a Pydantic model
when attempting to run the application.
To Reproduce
from fastapi import FastAPI
from pydantic import BaseModel
class T(BaseModel):
a: int
app = FastAPI()
@app.get("/", responses={200: {"model": List[T]}})
def index():
... # This is never entered due to error when adding the route
Expected behavior
This behaves the same as response_model=
since it is allowed and works as intended there.
Environment:
- OS: macOS
- FastAPI Version [e.g. 0.3.0], get it with: fastapi==0.42.0
- Python 3.7.0
Additional Context I was going to return my response directly but would like the schema to be automatically done based on the pydantic model. I was following the steps in https://fastapi.tiangolo.com/tutorial/additional-responses/
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Using both pydantic models as response throws error
Response_model is static, it can not be switchable depends on data. Your Error message. pydantic.error_wrappers.
Read more >How we validate input data using pydantic
Pydantic raises a ValidationError when the validation of the model fails, stating which field, i.e. attribute, raised the error and why.
Read more >Pydantic - The Blue Book
Pydantic. Pydantic is a data validation and settings management using python type annotations. pydantic enforces type hints at runtime, and provides user ...
Read more >Many-To-Many Relationships In FastAPI - GormAnalysis
Now let's set up pydantic models which we can use as response models in our FastAPI path operation functions. (Recall, pydantic models give ......
Read more >Why I use attrs instead of pydantic - The Three of Wands
Your Pydantic model cannot have a field named json , since that is one of the methods on the BaseModel . Can you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The “model” for the response should be the actual type returned with the response.
response_model
is basically a shorthand for what to use in the case where everything goes as planned, but, for example, if you wanted to return a list of errors in the case of a 400, you’d want to haveresponses={400: {"model": List[ErrorModel]}}
.It seems to me like you are concerned with the schema making sense as a human, but the point is for it to be correct if interpreted by a machine (e.g., for client generation).
I don’t see any reason why this version:
shouldn’t result in the same openapi schema as this version:
My suspicion is that this is only broken right now because most people don’t frequently use this capability, and so there hasn’t been a push to fix it. I don’t see any reason an arbitrary pydantic field couldn’t be supported here.
Admittedly, I’m not 100% confident in my understanding of the
responses
argument, but looking through the code it looks like it is intended to be treated the same as theresponse_model
(withresponse_model
just getting automatically injected into the responses with status code 200).This was fixed by @patrickmckenna in https://github.com/tiangolo/fastapi/pull/1017.
It will be available in the next release. 🚀 🎉