pydantic-v2

Write and modernize Pydantic v2 models, validators, serialization, and settings in Python.

1|1|Updated May 24, 2026
One-click install
npx skills add https://github.com/bm629/agent-skills --skill pydantic-v2-bm629
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pydantic-v2
Source: https://github.com/bm629/agent-skills/tree/main/skills/pydantic-v2
Command: npx skills add https://github.com/bm629/agent-skills --skill pydantic-v2-bm629

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pydantic, pydantic-settings, and includes references (resource) components.

What problem does it solve? Pydantic v1 idioms like class Config, .dict(), and @validator silently linger in codebases, producing deprecated or broken code. This Skill provides the current v2 API surface — ConfigDict, model_dump(), @field_validator, pydantic-settings — so generated or migrated code is correct and idiomatic. ## Core Features & Use Cases - Model definition and validation: Build BaseModel classes with typed fields, Annotated[..., Field(...)] constraints, @field_validator/@model_validator logic, and discriminated unions. - Serialization and configuration: Control output with model_dump* options, @field_serializer, @computed_field, aliases, and ConfigDict options like strict, frozen, and from_attributes. - Settings and migration: Load environment-based config via pydantic-settings, validate non-model shapes with TypeAdapter, and convert v1-era code using a full rename table. - Use Case: You inherit a service whose models still use class Config and .dict(). Ask the agent to modernize them — every model is rewritten to v2 idioms, with Optional defaults fixed and validators converted. ## Quick Start Use the pydantic-v2 skill to rewrite this v1-style model with class Config and .dict() calls into current Pydantic v2 idioms.

Frequently Asked Questions about pydantic-v2

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate Pydantic v1 code to v2?

Replace .dict() with model_dump(), .parse_obj() with model_validate(), @validator with @field_validator plus @classmethod, and the nested class Config with model_config = ConfigDict(...). Also fix Optional fields, which no longer get an implicit None default in v2.

How do I validate fields in Pydantic v2?

Use @field_validator with @classmethod directly beneath it for single-field rules, and @model_validator(mode="after") operating on self for cross-field checks. Modes before, after, plain, and wrap control when validation runs relative to parsing.

Does Pydantic v2 include BaseSettings?

No, BaseSettings moved to the separate pydantic-settings package in v2. Install it with pip install pydantic-settings and import BaseSettings and SettingsConfigDict from pydantic_settings to load configuration from environment variables and .env files.

Why is my Optional field still required in Pydantic v2?

In v2, Optional[int] only marks a field nullable; it does not supply a default, so the field remains required. Write int | None = None to make it both nullable and optional.

When should I use TypeAdapter instead of a model?

Use TypeAdapter to validate non-model shapes like list[Model], dicts, or primitives without defining a wrapper model. Build the adapter once and reuse it for hot paths, but never use it as a field annotation inside a BaseModel.