Pydantic, by Running It
Type annotations that actually do something. Validation, coercion and serialisation, each one runnable on the page.
About this track
A Python type annotation does nothing at runtime. Write <code>def price(x: int)</code> and Python will hand your function the string "cat" without a word of complaint. Pydantic is the library that makes those annotations real - it reads them, checks the data against them, and tells you precisely what was wrong when it does not fit.
This track works up from a first model to validators, serialisation and schemas. Every idea arrives as a small program you can run and edit on the page, because the fastest way to settle what Pydantic does with a value is to hand it one and look.
Every model on these pages runs in the browser against Pydantic itself, including the ones written to fail - because the error a library gives you is part of learning it, and reading one is a skill the documentation cannot teach.
What you will be able to do
- Describe the shape of incoming data as a model, and let the library reject what does not fit.
- Read a validation error well enough to know which field failed and why.
- Choose between strict and lax coercion deliberately, rather than discovering the default.
- Validate and serialise nested models, custom types, dates and decimals without hand-written checks.
- Recognise the v1 patterns still all over the internet, and their v2 replacements.
How the track is ordered
Models and fields come first, then validation - what happens automatically, what you have to ask for, and how to read the error when it fails. Types follow: nested models, optionals, enums, dates, decimals and the custom types that the standard library does not cover. Then the parts that shape a real application - settings, aliases, serialisation, generics and the FastAPI integration - and finally performance and the v1-to-v2 migration you will meet in existing code.
Where this leads
Pydantic is the validation layer under FastAPI, so the FastAPI track assumes what is here. It is also the boundary layer for anything that reads JSON, environment variables or a config file.
All 30 modules, in teaching order
- 01What Pydantic Is ForSee how Pydantic turns Python type annotations into runtime checks that reject bad data the moment it arrives.
- 02Your First BaseModelDefine a Pydantic BaseModel, create an instance from raw data, trigger a validation error on purpose, and export the result as a dict.
- 03Types and CoercionSee exactly what Pydantic will and will not convert: the coercion rules for strings, ints, floats and booleans, one example at a time.
- 04Required, Optional and DefaultsLearn why Optional does not mean optional in Pydantic, and how to distinguish required, defaulted and nullable fields correctly.
- 05Reading a ValidationErrorRead a Pydantic ValidationError line by line, understand loc, msg and type, and turn the report into a message your caller can use.
- 06Field ConstraintsUse Field to set min, max, regex and other constraints that close the gap between what a type allows and what your domain accepts.
- 07Pydantic vs DataclassesCompare Pydantic BaseModel with Python dataclasses side by side and see that validation at creation time is the deciding factor.
- 08Nested ModelsCompose models inside models so nested JSON validates recursively, with error paths that name the exact field at every depth.
- 09Collections of ModelsValidate lists, dicts, sets and tuples of models, and get error messages that point to the exact index or key that failed.
- 10Unions and Discriminated UnionsHandle fields that accept multiple shapes with Union, and add a discriminator so Pydantic picks the right branch without guessing.
- 11Enums and LiteralsRestrict a field to a fixed set of values using Enum or Literal, and see when each approach fits your data model best.
- 12Dates, UUIDs and DecimalsParse ISO dates, UUIDs and Decimals from raw strings, and avoid the two conversion traps that silently produce wrong numbers.
- 13Strict vs Lax ModeChoose between strict and lax validation per field or per model, and learn where coercion helps at the edge and hides bugs inside.
- 14field_validatorWrite custom validation and normalisation logic with field_validator, and see exactly where each mode runs in the pipeline.
- 15model_validatorWrite validators that compare multiple fields at once using model_validator, and choose between before and after modes.
- 16Computed FieldsAdd read-only fields that compute their value from other attributes and appear in serialized output without being part of input.
- 17model_configSet model-wide behaviour with model_config: extra keys, frozen instances, strict mode and assignment validation in one place.
- 18Annotated and Custom TypesDefine a validation rule once with Annotated and reuse it across models instead of copying the same constraint into every field.
- 19Validator ModesCompare before, after, plain and wrap validator modes, and see how each one interacts with Pydantic's coercion step.
- 20model_dump and model_dump_jsonExport a model with model_dump and model_dump_json, apply include and exclude filters, and avoid the TypeError most people hit first.
- 21AliasesMap camelCase payloads, reserved Python names and legacy fields to clean attribute names using aliases and AliasPath.
- 22Parsing JSONParse JSON directly into a model with model_validate_json and see why it is faster and stricter than json.loads plus model_validate.
- 23Custom SerializersControl how individual fields or entire models serialize, and understand when leaving the default alone is the better choice.
- 24JSON SchemaGenerate a JSON Schema from any Pydantic model and see why that automatic document powers OpenAPI, FastAPI and every tool that reads your API.
- 25TypeAdapterValidate any type annotation without building a model, using TypeAdapter for standalone checks on lists, dicts and primitives.
- 26Generic ModelsBuild one generic model for paginated responses, wrapped results or envelopes, then reuse it across every resource type.
- 27Settings ManagementLoad configuration from environment variables into a typed Pydantic model that validates every value before your application starts.
- 28Pydantic with FastAPISee how FastAPI turns Pydantic models into request validation, response shaping and automatic API documentation with no extra code.
- 29Performance and pydantic-coreSee why Pydantic v2 validates at Rust speed, where the time goes, and which three habits make the biggest performance difference.
- 30Migrating v1 to v2Walk through the renames, the silent behaviour changes and the patterns that break when moving a Pydantic v1 codebase to v2.