Skip to content

📣 📨 🖼 💽

👆 💪 📣 🖼 💽 👆 📱 💪 📨.

📥 📚 🌌 ⚫️.

Pydantic schema_extra

👆 💪 📣 example Pydantic 🏷 ⚙️ Config & schema_extra, 🔬 Pydantic 🩺: 🔗 🛃:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "Foo",
                    "description": "A very nice Item",
                    "price": 35.4,
                    "tax": 3.2,
                }
            ]
        }
    }


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "Foo",
                    "description": "A very nice Item",
                    "price": 35.4,
                    "tax": 3.2,
                }
            ]
        }
    }


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

👈 ➕ ℹ 🔜 🚮-🔢 🎻 🔗 👈 🏷, & ⚫️ 🔜 ⚙️ 🛠️ 🩺.

Tip

👆 💪 ⚙️ 🎏 ⚒ ↔ 🎻 🔗 & 🚮 👆 👍 🛃 ➕ ℹ.

🖼 👆 💪 ⚙️ ⚫️ 🚮 🗃 🕸 👩‍💻 🔢, ♒️.

Field 🌖 ❌

🕐❔ ⚙️ Field() ⏮️ Pydantic 🏷, 👆 💪 📣 ➕ ℹ 🎻 🔗 🚶‍♀️ 🙆 🎏 ❌ ❌ 🔢.

👆 💪 ⚙️ 👉 🚮 example 🔠 🏑:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str = Field(examples=["Foo"])
    description: Union[str, None] = Field(default=None, examples=["A very nice Item"])
    price: float = Field(examples=[35.4])
    tax: Union[float, None] = Field(default=None, examples=[3.2])


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str = Field(examples=["Foo"])
    description: str | None = Field(default=None, examples=["A very nice Item"])
    price: float = Field(examples=[35.4])
    tax: float | None = Field(default=None, examples=[3.2])


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

Warning

🚧 🤯 👈 📚 ➕ ❌ 🚶‍♀️ 🏆 🚫 🚮 🙆 🔬, 🕴 ➕ ℹ, 🧾 🎯.

example & examples 🗄

🕐❔ ⚙️ 🙆:

  • Path()
  • Query()
  • Header()
  • Cookie()
  • Body()
  • Form()
  • File()

👆 💪 📣 💽 example ⚖️ 👪 examples ⏮️ 🌖 ℹ 👈 🔜 🚮 🗄.

Body ⏮️ example

📥 👥 🚶‍♀️ example 📊 ⌛ Body():

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


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


@app.put("/items/{item_id}")
async def update_item(
    item_id: int,
    item: Item = Body(
        examples=[
            {
                "name": "Foo",
                "description": "A very nice Item",
                "price": 35.4,
                "tax": 3.2,
            }
        ],
    ),
):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(
    item_id: int,
    item: Item = Body(
        examples=[
            {
                "name": "Foo",
                "description": "A very nice Item",
                "price": 35.4,
                "tax": 3.2,
            }
        ],
    ),
):
    results = {"item_id": item_id, "item": item}
    return results

🖼 🩺 🎚

⏮️ 🙆 👩‍🔬 🔛 ⚫️ 🔜 👀 💖 👉 /docs:

Body ⏮️ 💗 examples

👐 👁 example, 👆 💪 🚶‍♀️ examples ⚙️ dict ⏮️ 💗 🖼, 🔠 ⏮️ ➕ ℹ 👈 🔜 🚮 🗄 💁‍♂️.

🔑 dict 🔬 🔠 🖼, & 🔠 💲 ➕1️⃣ dict.

🔠 🎯 🖼 dict examples 💪 🔌:

  • summary: 📏 📛 🖼.
  • description: 📏 📛 👈 💪 🔌 ✍ ✍.
  • value: 👉 ☑ 🖼 🎦, ✅ dict.
  • externalValue: 🎛 value, 📛 ☝ 🖼. 👐 👉 5️⃣📆 🚫 🐕‍🦺 📚 🧰 value.
from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


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


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int,
    item: Item = Body(
        examples=[
            {
                "name": "Foo",
                "description": "A very nice Item",
                "price": 35.4,
                "tax": 3.2,
            },
            {
                "name": "Bar",
                "price": "35.4",
            },
            {
                "name": "Baz",
                "price": "thirty five point four",
            },
        ],
    ),
):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int,
    item: Item = Body(
        examples=[
            {
                "name": "Foo",
                "description": "A very nice Item",
                "price": 35.4,
                "tax": 3.2,
            },
            {
                "name": "Bar",
                "price": "35.4",
            },
            {
                "name": "Baz",
                "price": "thirty five point four",
            },
        ],
    ),
):
    results = {"item_id": item_id, "item": item}
    return results

🖼 🩺 🎚

⏮️ examples 🚮 Body() /docs 🔜 👀 💖:

📡 ℹ

Warning

👉 📶 📡 ℹ 🔃 🐩 🎻 🔗 & 🗄.

🚥 💭 🔛 ⏪ 👷 👆, 👈 💪 🥃, & 👆 🎲 🚫 💪 👉 ℹ, 💭 🆓 🚶 👫.

🕐❔ 👆 🚮 🖼 🔘 Pydantic 🏷, ⚙️ schema_extra ⚖️ Field(example="something") 👈 🖼 🚮 🎻 🔗 👈 Pydantic 🏷.

& 👈 🎻 🔗 Pydantic 🏷 🔌 🗄 👆 🛠️, & ⤴️ ⚫️ ⚙️ 🩺 🎚.

🎻 🔗 🚫 🤙 ✔️ 🏑 example 🐩. ⏮️ ⏬ 🎻 🔗 🔬 🏑 examples, ✋️ 🗄 3️⃣.0️⃣.3️⃣ ⚓️ 🔛 🗝 ⏬ 🎻 🔗 👈 🚫 ✔️ examples.

, 🗄 3️⃣.0️⃣.3️⃣ 🔬 🚮 👍 example 🔀 ⏬ 🎻 🔗 ⚫️ ⚙️, 🎏 🎯 (✋️ ⚫️ 👁 example, 🚫 examples), & 👈 ⚫️❔ ⚙️ 🛠️ 🩺 🎚 (⚙️ 🦁 🎚).

, 👐 example 🚫 🍕 🎻 🔗, ⚫️ 🍕 🗄 🛃 ⏬ 🎻 🔗, & 👈 ⚫️❔ 🔜 ⚙️ 🩺 🎚.

✋️ 🕐❔ 👆 ⚙️ example ⚖️ examples ⏮️ 🙆 🎏 🚙 (Query(), Body(), ♒️.) 📚 🖼 🚫 🚮 🎻 🔗 👈 🔬 👈 💽 (🚫 🗄 👍 ⏬ 🎻 🔗), 👫 🚮 🔗 ➡ 🛠️ 📄 🗄 (🏞 🍕 🗄 👈 ⚙️ 🎻 🔗).

Path(), Query(), Header(), & Cookie(), example ⚖️ examples 🚮 🗄 🔑, Parameter Object (🔧).

& Body(), File(), & Form(), example ⚖️ examples 📊 🚮 🗄 🔑, Request Body Object, 🏑 content, 🔛 Media Type Object (🔧).

🔛 🎏 ✋, 📤 🆕 ⏬ 🗄: 3️⃣.1️⃣.0️⃣, ⏳ 🚀. ⚫️ ⚓️ 🔛 ⏪ 🎻 🔗 & 🏆 🛠️ ⚪️➡️ 🗄 🛃 ⏬ 🎻 🔗 ❎, 💱 ⚒ ⚪️➡️ ⏮️ ⏬ 🎻 🔗, 🌐 👫 🤪 🔺 📉. 👐, 🦁 🎚 ⏳ 🚫 🐕‍🦺 🗄 3️⃣.1️⃣.0️⃣,, 🔜, ⚫️ 👍 😣 ⚙️ 💭 🔛.