skip to content

Pydantic

Python validation driven by type hints: models and adapters that parse, coerce and serialize, with settings built on the same machinery. Asked because FastAPI and many data tools run on Pydantic.

on this pageshow

explore

questions

page 2 of 2

In Pydantic 2, how do before, after, plain and wrap field validators differ in what they receive and when they run?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Pydantic 2 field validator modes place your function around its type check: before gets raw input that is then type-checked, after gets the typed value, plain replaces the check entirely, and wrap gets a handler it may call, retry or skip.

open as a page

Would you make Pydantic 2's strict mode the team default for inbound models, and what would that policy cost your services?

level: principalimportance: should knowfreq 24%

basics

~20 s

Pydantic 2's strict mode suits JSON contracts between services you own, validated with model_validate_json; text-native inputs usually need lax mode. Most teams land on strict risky fields plus a strict base model, rolled out after measuring rejections.

open as a page

As the lead of a large Pydantic 1 codebase, how would you plan its move to Pydantic 2, and what would you refuse to do in a single step?

level: principalimportance: should knowfreq 32%

basics

~20 s

Stage it: switch imports to pydantic.v1 while still on version 1, upgrade the package with no behaviour change, then migrate clusters of linked models one at a time with bump-pydantic and payload-diff tests. Never upgrade and change behaviour in one step.

open as a page

Why does a Pydantic 2 generic `Page[T]` model return plain dicts or lose fields when built unparametrized, and what fixes it?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

An unparametrized Pydantic 2 generic validates its type variable as its bound, constraints or default, and otherwise as Any. So items stay raw dicts, or shrink to the bound's fields. Validate with Page[LineItem] instead.

open as a page

In Pydantic 2, why can model_json_schema() differ between mode='validation' and mode='serialization', and which one should document an event you publish?

level: seniorimportance: nice to knowfreq 24%

basics

~20 s

Pydantic 2's default validation-mode schema describes what the model accepts, while serialization mode describes what a dump writes: Decimal as a string only, computed fields as readOnly, custom serializers' return types. A published event is documented in serialization mode.

open as a page

showing 31–35 of 35