Response Models & Status Codes in Python
From the FastAPI cheat sheet ยท Request Body & Pydantic Models ยท verified Jul 2026
Response Models & Status Codes
Control response shape and HTTP status codes.
python
from fastapi import FastAPI, status
class ItemOut(BaseModel):
name: str
price: float
# password field excluded from response
@app.post("/items/", response_model=ItemOut, status_code=201)
async def create_item(item: Item):
return item๐ก response_model filters the output โ use it to hide internal fields like passwords
โก You can use return type annotations (-> ItemOut) instead of response_model=
๐ status.HTTP_201_CREATED is clearer than the magic number 201
๐ข response_model_exclude_unset=True omits fields that weren't explicitly set
responsestatus-codesmodels