How do you define a Pydantic model and use it in FastAPI?

Whether you use Pydantic BaseModel for request body validation in FastAPI.
Subclass BaseModel with name str and age int, then type-hint the parameter with the model.
What's really being asked
This question checks whether you understand FastAPI's core dependency on Pydantic for request body handling. The interviewer wants to see that you know declarative validation beats manual parsing, and that you understand Python type hints drive FastAPI's dependency injection. Specifically, they are looking for familiarity with BaseModel subclassing, field type annotations, and the convention of using the model itself as a path operation parameter.
The full answer
A strong response walks through four steps in order. First, import BaseModel from pydantic. Second, define a class that inherits from BaseModel and annotate the fields: name as a str and age as an int. Third, in the path operation function, declare a parameter with the Pydantic model as its type hint, for example async def create_user(user: UserModel). Fourth, mention that FastAPI automatically reads the request body, validates the JSON against the schema, converts types, and generates OpenAPI documentation. You should also note that invalid payloads return a 422 Unprocessable Entity without writing any explicit error handling.
The mistakes people make
Red flags include suggesting manual JSON parsing with request.json() or json.loads, which bypasses validation and documentation generation. Another mistake is defining the model but forgetting to type-hint the parameter, which causes FastAPI to treat it as a query parameter or path parameter instead of a request body. Using plain Python dataclasses without Pydantic integration or returning dictionaries instead of model instances are also signals that you have not used FastAPI's request body features in production.
What usually comes next
The interviewer may ask how to make fields optional or provide default values, which you answer with standard Python default values or Optional types. They might ask about nested models, where you define one BaseModel that contains another as a field type. Another common follow-up is how to add extra validation beyond types, such as minimum age constraints, which leads to Field() from pydantic or annotated types. They could also ask how to combine request body parameters with path and query parameters in the same function.
A concrete example
You could say: from pydantic import BaseModel; class User(BaseModel): name: str; age: int. Then in the route: from fastapi import FastAPI; app = FastAPI(); @app.post("/users/"); async def create_user(user: User): return user. When a client POSTs JSON like {"name": "Alice", "age": 30}, FastAPI validates the types, converts the integer, and injects a User instance. If the client sends {"name": "Alice", "age": "thirty"}, FastAPI returns a 422 error detailing the validation failure.
Interview question
You define a User model inheriting from BaseModel with name: str and age: int. What missing step lets FastAPI automatically validate an incoming JSON request body against it?
- a.Add response_model=User to the route decorator so FastAPI validates incoming data against it
- b.Manually parse request.json() and instantiate User inside the route function
- c.Define User as a Python dataclass and type-hint the parameter with that class
- d.Type-hint a path operation parameter with User, e.g., async def create_user(user: User)Correct
Why? this is the answer
FastAPI treats a parameter type-hinted with a BaseModel subclass as the request body and validates it automatically. Option B is tempting but wrong because parsing the body manually with request.json() skips Pydantic validation and prevents OpenAPI documentation generation.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #pydantic
- #python
- #validation
- #request-body
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