How do you define a Pydantic model for FastAPI request body validation?

Schema validation via Python type hints.
Subclass BaseModel with id int, email str, full_name str|None; pass it as a route param so FastAPI validates JSON and returns 422s.
Saying manual request.json() parsing.
What's really being asked
This question checks whether you treat API boundaries as schema contracts rather than loose dictionaries. Even at senior levels, interviewers want to see that you understand declarative validation, type safety, and framework ergonomics. It also reveals if you know the difference between Pydantic as a modeling layer and FastAPI as the HTTP layer that consumes it, and whether you can articulate the automatic error handling that replaces manual try except blocks.
The full answer
Four things in order. First, import BaseModel from pydantic. Second, define a class that inherits from BaseModel and annotate id as int, email as str, and full_name as Optional[str] or str | None. Third, declare the model as a parameter in the path operation function, for example async def create_user(user: User):. Fourth, explain that FastAPI inspects the type annotation, expects a JSON body, parses it, validates field types and requiredness, and either injects a populated instance or returns a 422 Unprocessable Entity with detailed error locations.
The mistakes people make
Three red flags stand out. One, saying you manually call await request.json() and then feed it into the model constructor; this ignores FastAPI's automatic dependency injection and validation pipeline. Two, using standard dataclasses without Pydantic and claiming FastAPI will still validate fields; it will not. Three, forgetting that optional fields need an explicit None default or Optional annotation, which causes the field to be treated as required and leads to unexpected 422 errors.
What usually comes next
The interviewer may ask how you handle nested models, how to add field constraints like email regex or min length, how Pydantic v2 differs from v1 in validation behavior, or how FastAPI generates OpenAPI schemas from these models automatically without extra configuration.
A concrete example
from pydantic import BaseModel; class User(BaseModel): id: int; email: str; full_name: str | None = None; from fastapi import FastAPI; app = FastAPI(); @app.post("/users/"); async def create_user(user: User): return user. When a client posts JSON missing the email key, FastAPI responds with a 422 and a JSON error body pointing exactly at the email field without any custom error handling code.
Interview question
Which method correctly enables automatic JSON body validation in a FastAPI route?
- a.Subclass BaseModel and declare it as the type of a path operation function parameterCorrect
- b.Subclass BaseModel and set it as the response_model in the route decorator
- c.Use a standard Python dataclass as the type annotation for the route parameter
- d.Call await request.json() manually and instantiate the Pydantic model inside the handler
Why? this is the answer
FastAPI inspects path operation parameter type annotations to automatically parse and validate incoming JSON against a Pydantic BaseModel. Manually calling request.json() bypasses this automatic pipeline, and response_model only defines the outgoing response schema rather than request validation.
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 python — each one lists the topics its interview covers.
See open roles