question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Unable to have List of Pydantic Model as route 'responses' model

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
dmontagucommented, Oct 17, 2019

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 have responses={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:

@app.get("/", responses={200: {"model": List[T]}})
def index():
    ... # This is never entered due to error when adding the route

shouldn’t result in the same openapi schema as this version:

@app.get("/", response_model=List[T])
def index():
    ... # This is never entered due to error when adding the route

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 the response_model (with response_model just getting automatically injected into the responses with status code 200).

2reactions
tiangolocommented, Feb 29, 2020

This was fixed by @patrickmckenna in https://github.com/tiangolo/fastapi/pull/1017.

It will be available in the next release. 🚀 🎉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found