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 pageshowhide
explore
- Models and Fields6 questions
- Validators5 questions
- Strict vs Lax Coercion6 questions
- Dumping & Serializers6 questions
- Settings Management6 questions
- V1 to V2 Migration6 questions
questions
page 2 of 2In Pydantic 2, how do before, after, plain and wrap field validators differ in what they receive and when they run?
basics
~20 sPydantic 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.
Would you make Pydantic 2's strict mode the team default for inbound models, and what would that policy cost your services?
basics
~20 sPydantic 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.
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?
basics
~20 sStage 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.
Why does a Pydantic 2 generic `Page[T]` model return plain dicts or lose fields when built unparametrized, and what fixes it?
basics
~10 sAn 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.
In Pydantic 2, why can model_json_schema() differ between mode='validation' and mode='serialization', and which one should document an event you publish?
basics
~20 sPydantic 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.
showing 31–35 of 35