py-pydantic-patterns

Migrate Python data models from Pydantic v1 to v2 with validators and serialization.

Updated Dec 22, 2025
One-click install
npx skills add https://github.com/CJHarmath/claude-agents-skills --skill py-pydantic-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: py-pydantic-patterns
Source: https://github.com/CJHarmath/claude-agents-skills/tree/main/skills/py-pydantic-patterns
Command: npx skills add https://github.com/CJHarmath/claude-agents-skills --skill py-pydantic-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides practical migration and pattern guidance for migrating from Pydantic v1 to v2, enabling robust validation, serialization, and API modeling in Python projects.

Core Features & Use Cases

  • Migration patterns: from v1 to v2 using field_validator, model_validator, and ConfigDict configurations.
  • Validation patterns: field validators, model validators, discriminated unions, and custom types for clean data models.
  • Serialization control: customize how models serialize to dicts or JSON with field_serializer and computed fields.
  • API modeling: build clear input/output schemas and reuse base models via schema inheritance for FastAPI/SQLModel.

Quick Start

Define a simple Pydantic v2 model with model_config, then validate input data using model_validate to demonstrate field and model validators.

Frequently Asked Questions about py-pydantic-patterns

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

FAQPage Schema
How do I migrate validators from Pydantic v1 to v2?

Migrate Pydantic v1 validators to v2 by replacing @validator with field_validator and @root_validator with model_validator. Configure models using ConfigDict and model_config for strict typing and reliable serialization.

What is the best way to customize JSON serialization in Pydantic v2?

Customize Pydantic v2 serialization by applying field_serializer to control dict or JSON output for specific fields. Add computed fields to include dynamically derived values in serialized API schemas.

How do field_validator and model_validator work together in Pydantic?

field_validator checks individual data fields during validation, while model_validator executes cross-field validation across the entire model. Use model_validate to process input data and trigger both validation layers cleanly.

Can I use Pydantic v2 validation patterns with FastAPI and SQLModel?

Pydantic v2 validation patterns integrate with FastAPI and SQLModel by building clear input and output schemas. Reuse base models via schema inheritance to standardize API modeling and data processing pipelines.

How do I handle discriminated unions and custom types in Pydantic v2?

Handle discriminated unions in Pydantic v2 to route validation logic based on specific field values, and define custom types for clean data models. This ensures strict typing across complex backend service payloads.