PythiloLesson 4 of 14 · progress kept in this browser

Filter output through a response model

Input validation and output shaping solve different problems. A service may hold internal fields that do not belong in an API response. A response model validates and serializes the public shape, filtering fields outside that declared contract.

Treat the public response type as a deliberate interface. Returning an internal dictionary directly can expose a field added much later, even if the endpoint itself was not edited. Test that a private field is absent as well as testing the public fields. Response validation is also useful for catching a server implementation that returns the wrong shape.

from fastapi import FastAPI
from pydantic import BaseModel
class Public(BaseModel):
    name: str
app = FastAPI()
@app.get("/me", response_model=Public)
async def me():
    return {"name": "Ada", "secret": "x"}
print(app.openapi()["paths"].keys())
Your task

Implement build_app() with GET /me and response model PublicUser(id:int,name:str). The endpoint may internally include token="private", but the HTTP JSON must contain only id=1 and name="Ada".

The first run loads fastapi, httpx, pydantic into this browser.