FastAPI response_model unable to handle Iterable types -- returns only the model keys
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import Any, Iterable, List
from fastapi import FastAPI, APIRouter
from pydantic import BaseModel
# Define simple model
class Simple(BaseModel):
spam: int
eggs: str
# instantiate fast api and route
loader = FastAPI(
version="0",
)
requests_router = APIRouter(prefix="/requests", tags=["requests"])
@requests_router.get("/", response_model=Iterable[Simple])
def get_requests() -> Iterable[Simple]:
return [
Simple(
spam=1,
eggs="a"
)
]
loader.include_router(requests_router)
###
### Output at http://127.0.0.1:8000/requests/ is
### {
### "spam": "eggs"
### }
###
Description
To reproduce
- Run uvicorn api2:loader --reload
- Go to http://127.0.0.1:8000/requests/
- Observe only the keys in the data model are returned
To fix
- Change the
response_model
toresponse_model=List[Simple]
- Go to http://127.0.0.1:8000/requests/
- Observe you get the correct response:
[
{
"spam": 1,
"eggs": "a"
}
]
I haven’t been able to find this in any documentation or bugs, but maybe my google skills are lacking
uvicorn 0.15.0
fastapi 0.68.1
Operating System
Windows
Operating System Details
Windows 11 21H2 (OS Build 22000.160))
FastAPI Version
0.68.1
Python Version
3.9.1
Additional Context
This has a workaround, but I don’t know the code base well enough to understand why an Iterable
type would cause this problem
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Response Model - FastAPI
FastAPI will use this response_model to: Convert the output data to its type declaration. Validate the data. Add a JSON Schema for the...
Read more >How do I send list of dictionary as Body parameter in FastAPI?
I think you should add a config class with orm_mode set to True in your Schema/Model class class DataModelOut(BaseModel): message: str ...
Read more >The Ultimate FastAPI Tutorial Part 4 - Pydantic Schemas
Pydantic describes itself as: Data validation and settings management using python type annotations. It's a tool which allows you to be much ...
Read more >tiangolo/fastapi - Gitter
ValueError: [TypeError("'_thread.lock' object is not iterable"), TypeError('vars() argument must ... and these are my request body model and response model
Read more >Many-To-Many Relationships In FastAPI - GormAnalysis
In this tutorial, I'll not only show you how to handle ... Modeling this kind of data in SQLAlchemy and FastAPI actually isn't...
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
Need this
The main issue is that the
jsonable_encoder
doesn’t support rawIterator
(produced by Pydantic when validating using as a response_modelIterable[Simple]
), but it does support List .I opened #3913 to try to fix the behavior - I tested in local your example code and it should work as you expect.