Pydantic Cheatsheet 04 — Serialization
Cheatsheet: model_dump, exclude/include/exclude_none/exclude_unset, field_serializer, model_serializer.
Cheatsheet: model_dump, exclude/include/exclude_none/exclude_unset, field_serializer, model_serializer.
Output side: model_dump, model_dump_json, field/model serializers, exclude/include, mode=python/json, by_alias.
Cheatsheet: discriminator field, callable Discriminator, Generic[T] models, recursive references.
Composing models: nesting, generics with TypeVars, discriminated unions, recursive models.
Cheatsheet: custom types via Annotated, GetCoreSchemaHandler, RootModel, TypeAdapter, dataclass integration.
Custom types via Annotated, RootModel for primitive-typed root, TypeAdapter for arbitrary types, dataclass integration.
Cheatsheet: lax default, strict per model / field / call, bool-is-int gotcha, JSON vs dict input.
When Pydantic coerces vs raises: strict mode, per-field strict, lax/strict tradeoffs at API boundaries.
Cheatsheet: alias variants, populate_by_name, AliasChoices for multi-alias, AliasPath for nested input.
JSON Schema from Pydantic: model_json_schema, OpenAPI integration, custom schemas, examples.