[Python] FastAPI's generated API docs drop down for showing multiple examples for the request...

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am following the FastAPI tutorial, specifically the part about displaying multiple examples: https://fastapi.tiangolo.com/tutorial/schema-extra-example/.

    I copied the code:

    from typing import Optional

    from fastapi import Body, FastAPI
    from pydantic import BaseModel

    app = FastAPI()


    class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None


    @app.put("/items/{item_id}")
    async def update_item(
    *,
    item_id: int,
    item: Item = Body(
    ...,
    examples={
    "normal": {
    "summary": "A normal example",
    "description": "A **normal** item works correctly.",
    "value": {
    "name": "Foo",
    "description": "A very nice Item",
    "price": 35.4,
    "tax": 3.2,
    },
    },
    "converted": {
    "summary": "An example with converted data",
    "description": "FastAPI can convert price `strings` to actual `numbers` automatically",
    "value": {
    "name": "Bar",
    "price": "35.4",
    },
    },
    "invalid": {
    "summary": "Invalid data is rejected with an error",
    "value": {
    "name": "Baz",
    "price": "thirty five point four",
    },
    },
    },
    ),
    ):
    results = {"item_id": item_id, "item": item}
    return results


    ...exactly as it is displayed on the page and I run it using Uvicorn. On my my screen I do not see any drop down with the 3 examples in the example dictionary of the request body. All I see is a single example value with just the types the fields need to be.

    Why would that be and why is this not working for me please?

    This is what I should get:

    [​IMG]

    But this is what I get:

    [​IMG]

    Continue reading...

Compartilhe esta Página