
Master the essentials of Pydantic v2 through a compact, code-dense course focused on real-world use, prerequisites, command line, virtual environments, decorators, type hints, json, and inheritance.
Explore Pydantic v2 essentials, from model basics and type coercion to validation, serialization, and custom validators, then master field aliases, specialized types, and complex model composition.
Access the companion GitHub repository for Pedantic Essentials, featuring annotated Jupyter notebooks with all course code, kept up to date; clone, download, or view online.
Learn how pedantic models enable deserialization, validation, and serialization in pydantic v2, and compare them with data classes as fastapi uses Json data.
Create a Pydantic model by inheriting from base model, define fields with type hints, and instantiate with named arguments; learn field validation, error reporting, and dynamic access via properties.
Explore deserialization by loading data into a pedantic Pydantic v2 model from a dictionary or json string, using model_validate and model_validate_json, with validation errors for missing fields.
Learn to serialize pydantic v2 models by converting them to dicts or json strings with model dump and model dump json, controlling fields with include or exclude.
Explore how Pydantic v2 handles type coercion during deserialization with pedantic models, comparing lax and strict modes and how validation enforces float and string field types.
Learn how to make fields optional in Pydantic v2 by assigning defaults, using a circle model with a 0,0 center, and understanding default validation and mutable default pitfalls.
Learn the difference between nullable and optional fields in pydantic v2, and declare int or None with union, optional, or the int or None syntax to control defaults and validation.
Explore how to inspect and differentiate default vs set values in Pydantic v2 models using model fields, instance inspection, and model fields set, including selective dumps.
Generate json schemas from Pydantic v2 models using model.json_schema, including handling optional and nullable fields, defaults, and field aliases for open api documentation with FastAPI.
Create a pydantic v2 automobile model with required fields and defaults, using date for manufacture_date and float for base_msrp. Validate serialization and deserialization with json and dict samples; verify errors.
Create a Pydantic v2 automobile model with required strings, nullable fields, a boolean default, date and float types, and test deserialization against expected serialization.
Learn how to customize the default Pydantic model configuration, overriding behaviors like ignoring extra fields, lax coercion, and unvalidated defaults, using a config dict attached via a model_config class attribute.
Explore how pydantic handles extra fields during deserialization by configuring model extra options: ignore, forbid, and allow, with practical examples and debugging tips.
master strict versus lax type coercion in pydantic v2, configure model strictness with a config dict, and see how json data and python objects are coerced and validated.
Enable model-level default value validation in Pydantic V2 to ensure defaults conform to field types and validators, preventing invalid defaults from slipping through.
Explore how pydantic v2 validates assignments by setting validate_assignment to true in model config to catch invalid values on assignment.
Explore mutability in Pydantic v2 by toggling model config to frozen, making models immutable, preventing field reassignment, and enabling hashed dictionary keys with hashable frozen instances.
Learn how Pydantic v2 can coerce numbers to strings using a model config, enabling lax mode to coerce integers, floats, and decimals, while strict mode prevents coercion.
demonstrates standardizing strings with pedantic by stripping whitespace and optionally converting to lower or upper case at the model level, noting last-wins precedence when both options are set.
Define color enums in Python and use them as fields in Pydantic v2 models. Deserialize from Python objects and JSON, validate allowed values, and serialize enums to their values.
Build on prior model to modify automobile model: forbid extra fields, strip whitespace, validate defaults and assignments with the provided enum for the type_ field; test by serializing and deserializing.
Apply pydantic v2 to validate and serialize a project solution by configuring model options, using model validate json and model dump to compare with the expected data serialization.
Discover how field aliasing manages snake_case and camelCase during serialization and deserialization in Pydantic v2. Explore field names, aliases, serialization aliases, validation aliases, and auto alias generation for data handling.
Configure fields with the field object to set aliases and defaults, enabling deserialization from aliases while Python uses field names, and serialize with by alias when needed.
Configure your Pydantic v2 models to auto generate aliases with a function, converting snake_case to camelCase, enabling by alias deserialization and serialization.
Learn to deserialize using either field name or alias by configuring the model with populate_by_name, and generate aliases with to_camel, enabling optional fields and controlled serialization by alias.
Explore how to use different aliases for deserialization and serialization in pydantic v2, handling external API field names while presenting clean json to your own users.
Explore how validation aliases work in Pydantic v2, how they differ from regular aliases and serialization aliases, and how to configure and override inputs and outputs during deserialization and serialization.
Implement Pydantic v2 project logic to use camelCase aliases for fields, map deserialization from data names to model fields, and format manufactured date as yyyyMMdd in json output.
Discover pedantic's specialized types and built-in validators, including positive int, con list, past date, http url, and email string, and learn how they validate and parse data.
Explore how the positive int pedantic type enforces a strictly positive integer in a pydantic v2 model, with examples of a circle's center and radius.
Define constrained lists with con list to enforce a two-to-three element center and a positive radius with a default. Review validation errors and deprecations like unique items and constraint strings.
Learn to use uuid4 with pydantic BaseModel fields of type Uuid4, using a default factory for unique ids, and understand json serialization and deserialization.
Master date related types in Pydantic v2, including past date time, aware and naive date times, and how to standardize on UTC to avoid time zone ambiguity.
Explore Pydantic v2 network types, including email and name email validation with the email validator, and any URL with http, https, or ftp, plus IPv4/IPv6 address handling.
Add id_ as the first field in the automobile model with a UUID type and default none, ensuring json serialization and alias-based deserialization in this pydantic v2 essentials project.
See how to add a first field id as a nullable UUID in a Pydantic v2 model, set alias id, and validate and serialize data using alias-based serialization and validation.
Learn how to use the field class in pydantic v2 to apply numerical constraints—gt, ge, lt, le, and multiples—demonstrating positive int equivalence and validation across numbers and dates.
Explore string constraints in pydantic v2, including min length, max length, and the use of regular expression patterns, with examples for strings, lists, and tuples.
Learn how default factories prevent mutable defaults in pydantic v2 by re-evaluating defaults for each instance, using field or lambda for datetime and empty collections.
Explore additional field configurations in Pydantic v2, including strict versus lax coercion, validating defaults, freezing fields, and excluding sensitive fields from serialization.
Apply pydantic v2 constraints to an automobile model: enforce a 1980-01-01 minimum manufacture date, 2-4 doors, and a non-null uuid default, with serialization and uniqueness tests.
Show how pydantic v2 enforces constraints: date >= 1980, doors as 2 or 4 with range and multiples, and non-null uuid4 default via a default factory, plus validation checks.
Discover annotated types in Python and how Pydantic uses them to attach metadata to existing types, enabling field, validators, and serializers in models.
Explore annotated types in Python and how Pydantic leverages them to attach constraints, validators, and serializers to model fields, including defining a reusable bounded int.
Examine annotated types and type variables in Python typing with pedantic. Apply a bounded list using a type variable to enforce max length, defaults, and homogeneous elements in pydantic models.
Explore string constraints in Pydantic v2, comparing field-level options with the string constraints class. Learn to apply min length, max length, pattern, and strip whitespace via annotations.
Create annotated bounded string and bounded list types in pydantic v2, use a bounded list via a type variable for automobile fields (including top_features), and test (de)serialization with small models.
Build robust Pydantic v2 models with annotated bounded strings and lists, test validation and deserialization, and implement a top features field for an automobile model with optional defaults.
Explore how custom validators in Pydantic v2 transform and validate input data, using before and after validators, execution order, and error handling with value errors.
Create custom validators in pydantic v2 with the field_validator decorator as after validators that run after type coercion, enabling validation with value errors or data transformation across fields.
Intercept raw input with before validators to customize deserialization before Pydantic validation, handle date time formats, and learn the bottom-to-top execution order of before validators.
Learn to combine before and after validators in Pydantic v2, control execution order, and convert date times to UTC after pedantic validation for consistent, timezone-aware results.
Explore Pydantic v2 custom validators with annotated types, including before and after validators, reusable validator patterns, and building a generic unique-element list validator across models with type variables.
Learn dependent field validations in Pydantic v2 using validation info to access prior validated fields and enforce end date after start date.
Build a Pydantic v2 project by extending a vehicle model with a nullable registration date (CamelCase serialization, date validation against manufactured date) and a dictionary-based, case-insensitive country validator.
Implement a nullable registration date with camel case aliasing and a shared serializer in a Pydantic v2 project, while validating the manufactured date order and registration country with after validators.
Explore using regular Python class properties and cached properties within Pydantic v2, and turn them into computed fields for serialization, while understanding when to keep properties non-serializable.
Learn how to attach methods and properties to Pydantic v2 models, compare area as a method versus a property, and cache area calculations with cached properties.
Turn a property into a serialized computed field using pydantic's computed field decorator, enabling area calculations to appear in representations and JSON outputs.
Implement a computed, cached registration country code using a flipped country name to code lookup, and prune the model representation to id, manufacturer, series_name, and type_.
Show how to solve a Pydantic v2 project using a cached property and a computed field to derive a registration country code. Explain removing fields from representations with repr=False and guarding database lookups.
Learn to implement custom serializers via annotated types, enabling reusable serializers across models, while preserving field serialization info and when-used behavior for dictionary and JSON outputs.
Demonstrates defining custom serializers for annotated types in Pydantic v2, including json-specific serialization and validators to ensure UTC-aware date times in models.
Create an annotated date type to extract the date serializer from the model, apply it to all date fields, and test by reusing data from the previous project.
Build a date serialization workflow with a serialize date helper and an annotated custom date using a plain serializer, then apply it to manufactured and registration dates.
This is an advanced level course on using the Pydantic V2 library. This course is not for beginners!
I have worked with Pydantic (starting with v1) for many years, and use that experience to bring you a course that focuses on the essential parts of Pydantic you will need to know to use it professionally, effectively and to leverage it's full potential.
Pydantic provides a very flexible framework for modeling, validating and parsing data in Python.
Although Pydantic is often associated with frameworks such FastAPI, it has far broader applications well beyond just REST API development. From modeling and validating data in databases (like Redis, DynamoDB, Clickhouse), queues (like SQS, ElasticMQ, RabbitMQ), and even CSV files, to even providing argument validation for your custom Python functions!
Pydantic is a very flexible, fast-to-develop, and easy-to-understand data modeling framework that belongs in every serious Python developer's toolkit.
Anytime you have a Python project that contains a fair amount of data validation and modeling into Python classes, Pydantic can be leveraged very effectively.
You can think of Pydantic as somewhat similar to Python's dataclasses, but with an advanced and flexible data validation layer, as well as the easy ability to deserialize (load) and serialize (output) these Python/Pydantic classes into plain dictionaries and JSON. Just like dataclasses, Pydantic uses Python's type hinting capabilities to define data models, but then adds in validation and serialization/deserialization capabilities, which are all fully customizable.