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

More Python tasks

Back to the full FastAPI cheat sheet