tezvyn:

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

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
How do you define a Pydantic model and use it in FastAPI?
WHAT IT TESTS

Whether you use Pydantic BaseModel for request body validation in FastAPI.

ANSWER OUTLINE

Subclass BaseModel with name str and age int, then type-hint the parameter with the model.

WHAT THIS TESTS: 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.

A GOOD ANSWER COVERS: 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.

COMMON WRONG ANSWERS: 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.

LIKELY FOLLOW-UPS: 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.

ONE 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.

Source: fastapi.tiangolo.com

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.