Careeroza
Response Models
response_model Filters Output
Example
Declare what the endpoint returns. Extra fields (like a password hash) are removed, and the docs show the exact response shape:
from fastapi import FastAPI
from pydantic import BaseModel
class UserIn(BaseModel):
username: str
password: str
class UserOut(BaseModel):
id: int
username: str
app = FastAPI()
@app.post("/users", response_model=UserOut, status_code=201)
def create_user(user: UserIn):
saved = {"id": 1, "username": user.username, "password_hash": "x9$..."}
return savedRequests and responses
POST /users {"username": "asha", "password": "s3cret!"}
→ 201 {"id": 1, "username": "asha"}Note: Always use separate input and output models for users: never return passwords or hashes.