Learn Pydantic

Data validation and parsing for Python, driven by type hints

15 lessons · 3 hours · 4 levels · covers Pydantic 2.13

Pydantic validates and converts data using classes whose fields are ordinary Python type hints. You describe the shape of your data once and get parsing, clear error messages, serialization and JSON Schema from it. It is the validation layer behind FastAPI, many LLM SDKs and countless config loaders, and its v2 core is written in Rust.

Before you start

Pydantic builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 48 min in total
2. PracticalUse it productively at work, with the usual idioms9 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs12 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know15 lessons · 3 hours in total

The Pydantic tree

PydanticYour first BaseModelYour first BaseModelField types, optionals and nestingField types, optionalsand nestingHandling ValidationErrorHandlingValidationErrorField() defaults and constraintsField() defaults andconstraintsDumping models to dicts and JSONDumping models todicts and JSONField and model validatorsField and modelvalidatorsModel configuration and strict modeModel configurationand strict modeAliases, computed fields and serializersAliases, computedfields and serializersSettings from the environmentSettings from theenvironmentClasses and instancesClasses andinstancesType hints basicsType hints basicsExceptions in depthExceptions in depth
Essentials and Practical shown (9 of 15 lessons); the full list is below. Click a lesson to read it.

All 15 lessons, in order

Models and fields

  1. Your first BaseModel · Essentials · 10 min

    Subclass BaseModel and annotate attributes; Pydantic validates and converts input when you create an instance.

  2. Field types, optionals and nesting · Essentials · 12 min

    Use standard type hints, Optional, collections and other models as field types to describe nested data.

  3. Handling ValidationError · Essentials · 8 min

    Catch ValidationError and use errors() to get every failing field with its location, message and type.

  4. Field() defaults and constraints · Essentials · 10 min

    Use Field to add constraints like gt, max_length and pattern, default factories, descriptions and examples.

Serialization and config

  1. Dumping models to dicts and JSON · Essentials · 8 min

    Turn models back into dicts or JSON with model_dump and model_dump_json, filtering fields with include, exclude and exclude_unset.

  2. Aliases, computed fields and serializers · Practical · 14 min

    Map external names with aliases, add computed_field properties and customize output with field_serializer.

  3. Settings from the environment · Practical · 10 min

    Load typed, validated configuration from environment variables and .env files with pydantic-settings' BaseSettings.

Validation

  1. Field and model validators · Practical · 14 min

    Add custom checks and transformations with @field_validator and @model_validator in before, after, wrap or plain mode.

  2. Model configuration and strict mode · Practical · 10 min

    Use model_config = ConfigDict(...) to forbid extra keys, freeze models, enable strict mode and validate on assignment.

Types beyond models

  1. Unions and discriminated unions · Advanced · 12 min

    Validate one-of-several shapes with unions, and use a Literal discriminator field for fast, precise results.

  2. TypeAdapter: validating any type · Advanced · 10 min

    Validate, dump and get JSON Schema for types that are not models, such as list[Item] or TypedDicts, with TypeAdapter.

  3. Reusable types with Annotated validators · Advanced · 12 min

    Build reusable constrained types with Annotated plus AfterValidator, BeforeValidator and PlainSerializer.

  4. Generic models · Expert · 12 min

    Parametrize models with TypeVars, like Page[T] or Response[T], and get real validation for each concrete type.

Internals and performance

  1. pydantic-core and custom types · Expert · 15 min

    Understand how models compile to a core schema run by Rust, and hook custom classes in with __get_pydantic_core_schema__.

  2. Performance tuning · Expert · 12 min

    Speed up Pydantic with model_validate_json, reusable adapters, precise types, discriminators and deferred schema builds.