DEV Community

Jeeva Madhaiyan
Jeeva Madhaiyan

Posted on

The following code from fastapi documentation under this subtitle "Response Model encoding parameters" seems to be not working

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float = 10.5
    tags: list[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]

Enter fullscreen mode Exit fullscreen mode

"""
the variable items has no effect with the path operation
and returns the key error
"""

Top comments (1)

Collapse
 
jeelion22 profile image
Jeeva Madhaiyan • Edited

There is no issue on them. they are right