Response Model

noun

say it: “rih-SPONS MOD-ul” /rɪˈspɒns ˈmɑdəl/

A model that describes what a FastAPI route sends back; FastAPI cuts the answer down to its fields, so private fields such as a password hash never leave.

What is Response Model?

A response model describes what an endpoint returns. FastAPI filters the output to match it, so private fields like password hashes never leak.

class UserOut(BaseModel):
    id: int
    name: str

@app.get('/users/{user_id}', response_model=UserOut)
def get_user(user_id: int):
    return find_user(user_id)  # password_hash is dropped

In a sentence

  • “With a response model, the docs show exactly what each answer looks like.”
  • “The response model dropped the applicant's internal notes from the answer.”

Level: Apprentice · More FastAPI terms at this level

Updated