tezvyn:

Create a generic Pydantic BaseModel for API response wrappers

AI-drafted, machine-checkedSource: pydantic.devadvanced
Create a generic Pydantic BaseModel for API response wrappers
WHAT IT TESTS

Pydantic v2 generics and OpenAPI schema generation.

ANSWER OUTLINE

subclass BaseModel and Generic[T]; type data as T; use ResponseWrapper[User]; note unparametrized TypeVars validate as Any.

WHAT THIS TESTS: This question probes whether you understand Pydantic v2 generic model mechanics, not just static typing. The interviewer wants to see if you know that BaseModel must explicitly inherit from Generic[T] to carry type parameters through validation and JSON schema generation. They also care whether you understand the runtime behavior of unparametrized TypeVars and how FastAPI or other frameworks expose these types in OpenAPI docs.

A GOOD ANSWER COVERS: Four things in order. First, import Generic and TypeVar from typing, create a TypeVar such as T equals TypeVar with the name T, and define class ResponseWrapper inheriting from BaseModel and Generic[T] with fields like data of type T and success of type bool. Second, show concrete usage by parametrizing the model, for example ResponseWrapper[UserOut], which gives both mypy and Pydantic enough information to validate the nested shape and emit a precise JSON schema. Third, explain that if you leave the wrapper unparametrized, Pydantic treats the TypeVar as Any at validation time and the generated OpenAPI schema may show an incomplete or overly permissive component. Fourth, mention that Pydantic v2 handles generic recursion and parametrized subclasses correctly, so you can subclass ResponseWrapper[T] further if needed.

COMMON WRONG ANSWERS: Three red flags stand out. One, using Union or Any instead of a TypeVar, which defeats the purpose of a reusable generic wrapper. Two, forgetting to include Generic[T] in the class bases and assuming BaseModel alone propagates type parameters, which breaks both runtime validation and schema generation. Three, claiming that Python generics are only static and do not affect Pydantic validation, when in fact Pydantic v2 resolves generic parameters at runtime to build validators.

LIKELY FOLLOW-UPS: The interviewer may ask how you would constrain T to Pydantic models only, perhaps with a bound or a custom protocol. They might ask what happens to OpenAPI docs when you return a list of ResponseWrapper[Item] from a FastAPI endpoint. Another follow-up is how to handle generic models with default factory fields or recursive type references inside the wrapper.

ONE CONCRETE EXAMPLE: Import Generic and TypeVar from typing and BaseModel from pydantic. Define T as TypeVar with the name T. Define class ResponseWrapper with bases BaseModel and Generic[T], containing success as bool, data as T, and error as an optional string defaulting to None. Define class UserOut with bases BaseModel, containing id as int and name as str. Concrete parametrization ResponseWrapper[UserOut] gives full schema generation and runtime validation. Instantiating it with success equals True and data as a UserOut instance with id 1 and name Ada produces a fully typed response object.

Source: pydantic.dev

Read the original → pydantic.dev

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.