What are FastAPI response models?

Answer

Response models in FastAPI define the schema of the response data. Declare with the response_model parameter on the route decorator: @app.get('/users/{id}', response_model=UserResponse). FastAPI validates the returned data against the Pydantic model and serializes it, automatically filtering out fields not in the response model — even if the underlying ORM object has sensitive fields like password_hash. Create separate request and response models: class UserCreate(BaseModel): name: str; password: str and class UserResponse(BaseModel): id: int; name: str. FastAPI also uses the response model for auto-documentation, showing the exact response schema in Swagger UI. Set response_model_exclude_unset=True to omit fields that were not set (useful for PATCH endpoints).