๐ฃ ๐จ ๐ผ ๐ฝ¶
๐ ๐ช ๐ฃ ๐ผ ๐ฝ ๐ ๐ฑ ๐ช ๐จ.
๐ฅ ๐ ๐ โซ๏ธ.
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๏ธโฃ,, ๐, โซ๏ธ ๐ ๐ฃ โ๏ธ ๐ญ ๐.