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๏ธโƒฃ,, ๐Ÿ”œ, โšซ๏ธ ๐Ÿ‘ ๐Ÿ˜ฃ โš™๏ธ ๐Ÿ’ญ ๐Ÿ”›.