tezvyn:

Model an Order with a nested Product list in Pydantic

AI-drafted, machine-checkedSource: pydantic.devintermediate
Model an Order with a nested Product list in Pydantic

It tests Pydantic nested model composition. Define Product as BaseModel, then Order with products: list[Product]; Pydantic recursively coerces each dict and raises ValidationError on failure. A red flag is insisting on manual iteration.

WHAT THIS TESTS: This question probes whether you understand declarative schema composition in Pydantic and how its validation engine handles nested data structures automatically. The interviewer wants to see that you know nested models are first-class citizens, not afterthoughts requiring manual parsing.

A GOOD ANSWER COVERS: First, define the child model Product by subclassing BaseModel and annotating name as a string and price as a float. Second, define the parent model Order with a field products annotated as a list of Product. Third, explain that passing a dictionary to Order.model_validate or the constructor triggers recursive validation where Pydantic instantiates each item in the list as a Product, coercing types and checking constraints. Fourth, note that if any element fails, Pydantic raises a ValidationError that includes the exact path such as products followed by the index of the offending item. Fifth, mention that this works because Pydantic treats annotated fields as part of the schema graph and resolves them during core schema generation, so no extra wiring is needed for standard nesting.

COMMON WRONG ANSWERS: A red flag is proposing a manual for-loop inside an init method or a custom validator to construct Product objects one by one, which ignores Pydantic's built-in recursive machinery. Another red flag is using untyped lists like list without parameterizing them with Product, which disables nested validation and treats items as opaque objects. A third red flag is confusing Pydantic validation with simple JSON deserialization, implying that the library only checks top-level keys and ignores inner structure or type safety.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle a variable number of nested items or optional fields inside Product. They might ask about performance implications of deep nesting or how to customize error messages for specific items. Another follow-up is how to use RootModel if the top-level payload is just a list of orders rather than a single object, or how to enforce unique product names within the list.

ONE CONCRETE EXAMPLE: Imagine an order payload with two products where the second product has a negative price. When Order.model_validate is called, Pydantic successfully creates the first Product, then detects the invalid float constraint on the second item. The resulting ValidationError contains a loc tuple pointing to products, index one, and price, making it trivial for an API consumer to pinpoint the issue without writing custom error logic or recursive inspection code.

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.