How do you prevent password_hash from appearing in a FastAPI response?

Tests FastAPI response filtering and the security practice of separating DB schemas from API contracts. A strong answer proposes a dedicated output model omitting the field, then cites response_model_exclude. Red flag: manual dict deletion or monkey-patching.
What's really being asked
FastAPI response model filtering and the security-critical separation between internal data models and external API contracts. Interviewers want to see that you prefer declarative serialization control over imperative workarounds, and that you understand how FastAPI uses Pydantic to guarantee what leaves the server.
The full answer
First, the preferred approach is to create a separate Pydantic output model that simply does not include the password_hash field at all. This keeps the database schema isolated from the API contract and prevents accidental leaks when new sensitive fields are added later. Second, mention the decorator-level option response_model_exclude which accepts a set of field names to omit during serialization. This is useful for quick fixes or legacy endpoints but is less maintainable than a dedicated model because the contract is not explicit in the type system. Third, note that FastAPI performs data filtering automatically when the return type or response_model is declared, so the conversion happens declaratively without manual intervention.
The mistakes people make
A major red flag is suggesting manual dictionary manipulation such as deleting the key before returning the model. Another is proposing runtime mutation of the Pydantic model or using monkey-patching to hide fields dynamically. Relying on client-side filtering is also unacceptable from a security standpoint. Finally, using the same model for both database representation and API responses without any exclusion mechanism shows a lack of architectural boundaries and invites data leaks.
What usually comes next
The interviewer might ask how you would handle a field that should be included on creation but hidden on read, which points to having separate input and output models. They might also ask about response_model_include versus response_model_exclude, or how to handle nested models where sensitive data appears at multiple levels. Another angle is asking how Pydantic version two serialization behavior or model configuration interacts with FastAPI's automatic filtering.
A concrete example
Suppose you have a UserDB model with id, email, and password_hash. Define a UserOut model with only id and email. In the FastAPI path operation decorator, set response_model=UserOut. When the endpoint returns a UserDB instance, FastAPI filters the data to match UserOut and password_hash never reaches the JSON response. Alternatively, you could keep a single model and use response_model_exclude with a set containing password_hash in the decorator, though this is riskier for long-term maintenance because the original model still carries the sensitive field.
Interview question
A FastAPI endpoint returns a UserDB model containing password_hash. Which strategy best prevents exposing the hash while keeping the API contract explicit and maintainable?
- a.Set password_hash to None in the endpoint body before returning the UserDB instance.
- b.Use response_model_exclude={"password_hash"} on the decorator to omit the field during serialization.
- c.Create a separate UserOut model without password_hash and set response_model=UserOut on the endpoint.Correct
- d.Call user.model_dump(exclude={"password_hash"}) and return the raw dict from the endpoint.
Why? this is the answer
A dedicated output model declaratively isolates the API contract from the database schema and prevents accidental leaks if new sensitive fields are added later. Option B is a tempting quick fix, but it keeps the sensitive field in the source model and hides the contract outside the type system, making it harder to maintain.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles