Good Code
The good version declares the public response shape and lets FastAPI serialize only the fields that belong in the contract.
Lesson 03
Return response models that expose the public API shape and filter internal fields.
from fastapi import APIRouter
from pydantic import BaseModel
router = APIRouter()
class ReviewResponse(BaseModel):
id: int
title: str
status: str
@router.get("/reviews/{review_id}", response_model=ReviewResponse)
async def get_review(review_id: int):
review = await reviews.find(review_id)
return reviewfrom fastapi import APIRouter
router = APIRouter()
@router.get("/reviews/{review_id}")
async def get_review(review_id: int):
review = await reviews.find(review_id)
return review.__dict__The good version declares the public response shape and lets FastAPI serialize only the fields that belong in the contract.
The bad version returns object internals. Private fields, database state, or unexpected attributes can leak into the API.