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.

FastAPI response_model unable to handle Iterable types -- returns only the model keys

See original GitHub issue

First 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

To fix

[
    {
        "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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
roj1512commented, Sep 1, 2022

Need this

1reaction
klaa97commented, Sep 17, 2021

The main issue is that the jsonable_encoder doesn’t support raw Iterator (produced by Pydantic when validating using as a response_model Iterable[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.

Read more comments on GitHub >

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

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