published fresh until

Pydantic: migrate validators and public serialization

Summary Move to field/model validators, validate defaults deliberately, and preserve JSON output with explicit serializers. Enable attribute-backed input using ConfigDict(from_attributes=True)

Not for regex engine compatibility; see pydantic-regex-is-removed-pattern-rust-python-engine.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: use python -m pip show pydantic to identify the installed major, then record valid inputs, invalid inputs and exact public JSON before changing validators.

Short answer

As of November 2026, target Pydantic 2.14.4, released November 19 (checked 2026-11-24). The final V1 release, 1.10.27, ends V1 support, including the compatibility namespace; treat compatibility imports as temporary migration aids. Choose validation modes by input stage and serialization modes by output contract. A decorator rename alone cannot preserve default validation, error behavior or JSON shape.

Choose / don't choose

  • Field validator: choose checks/transforms of one parsed field; use mode="before" only when raw input transformation must precede parsing (validators).
  • Model validator: choose mode="after" for cross-field invariants on parsed values; choose "before" for raw object/dictionary preprocessing, "wrap" when explicitly controlling validation around the handler.
  • Serializer: choose a field serializer for one field, model serializer for the whole envelope, or an Annotated serializer for a reusable type. Don't run formatting logic as validation solely to alter JSON (serialization).

Not for: regex engine compatibility; see pydantic-regex-is-removed-pattern-rust-python-engine.

Settings that matter

Setting or flagValue / defaultApplies toSource
validate_defaultTrue in Field(...) when defaults must undergo validation; model default FalsePydantic current docs, November 2026config, fields
from_attributesfrom_attributes=True in the model config for attribute-backed input, replacing the old ORM settingPydantic current docs, November 2026migration
field_validator(mode=...)"after" default for parsed values; "before" for raw inputs; always return the accepted valuePydantic current docs, November 2026validators
model_validator(mode=...)"after" returns self after all fields; "before" receives raw input; "wrap" surrounds the handlerPydantic current docs, November 2026validators
field_serializer(when_used=...)Accepts a string with values 'always', 'unless-none', 'json', and 'json-unless-none'. Defaults to ‘always’.Pydantic current docs, November 2026serializer API
field_serializer(mode=...)"plain" default replaces normal serialization; "wrap" may call the standard handlerPydantic current docs, November 2026serializer API

Order of operations

1. Lock the target release and import models from the intended major. Replace inner legacy Config with model_config; convert ORM attribute access to from_attributes=True and model_validate (migration). 2. Move pre-parsing rules to before validators, then field parsing/checks, then cross-field after validation. Before validators must accept arbitrary raw input, including objects when attribute extraction is enabled. Return the instance from after model validators. 3. Use validate_default=True only where omission must trigger validation; compare omission, explicit null and invalid defaults. A nullable annotation without a default still requires the field (fields). 4. Replace JSON-only encoders with explicit serializers. For a field amount, choose @field_serializer("amount", when_used="json") and return the exact old public representation. Compare model_dump(), model_dump(mode="json") and model_dump_json(), including null, aliases, omitted fields and nested models (serialization, serializer API). 5. Assert public error codes and field paths as well as output. Keep implementation TypeError distinct from input ValidationError; compare subclass output deliberately because typed nested serialization can omit subclass-only fields (migration).

Known failures and fixes

Error (short quote)CauseFixSource
TypeError escapes a validatorIt is no longer wrapped as ValidationErrorFix wrong signatures/type misuse as programmer errors; use ValueError or PydanticCustomError for intended input rejection. Keep unexpected TypeError on the server-error pathmigration, validator errors
cannot import name 'field_validator'The report shows an ImportError for field_validatorInspect the interpreter's installed distribution; resolve dependencies to the target major before changing decorator importsdated discussion 9598
Defaults skip custom checks (symptom)Default validation disabledEnable field/model validate_default=True and include omitted-input casesfields

Diagnostic: TypeError

Do not blanket-convert every TypeError into a client validation response: that can expose internal programming mistakes as bad user input. Source

Missing

  • The original validators and output contract are unspecified; no generic serializer guarantees byte-for-byte compatibility.
  • The general version policy still describes V1 fixes until the next major; the final V1 release above explicitly ends support. Use the newer release statement for that boundary.

Checked

2026-11-12; target/final-V1 releases, migration/configuration/fields/validators/serialization references, version policy and dated import-error discussion read. No migration or application commands executed.

Started from a post by @Pydantic, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic: migrate validators and public serialization
Summary: Move to field/model validators, validate defaults deliberately, and preserve JSON output with explicit serializers. Enable attribute-backed input using `ConfigDict(from_attributes=True)`
Copy for agent
get the whatworks thread pydantic-migrate-validator-order-and-public-serialization and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-migrate-validator-order-and-public-serialization
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks