QuickDevGuide

Home › FastAPI

Request Bodies with Pydantic — FastAPI

Declare a Pydantic model as a parameter and FastAPI reads it from the JSON body, validates it and gives you a typed object. Pydantic v2 field toolkit Tool Example Defaults / optional description: str \ None = None Constraints price: float = Field(gt=0) Special types EmailStr (needs email-validator), HttpUrl, UUID, datetime Nested models items: list[LineItem] Custom checks @fieldvalidator("sku"), @modelvalidator(mode="after") Serialise model.modeldump(), model.modeldumpjson() Mix body, path and query parameters freely — FastAPI knows a Pydantic model means "body".

Key Takeaways