# Build Pydantic v2 Models Correctly

Pydantic v1 habits can cause validation and serialization defects. This skill provides Pydantic v2 patterns for schemas, validators, configuration, unions, and serialization.

## Install

```bash
npx skillstore add cjharmath/py-pydantic-patterns
```

## Metadata

- Status: approved
- Slug: cjharmath-py-pydantic-patterns
- Skillstore revision: r1
- Version status: missing
- Tree hash: 023cd40a7aacff87d75a10f541da17ba101f2c459ec5d5b15533b354b1ae2c53
- Author: CJHarmath
- GitHub username: CJHarmath
- License: MIT
- Repository: https://github.com/CJHarmath/claude-agents-skills/tree/main/skills/py-pydantic-patterns
- Ref: dd4a3ef9f20ddf38830950b4bb713df96b431fd6
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands
- Quality score: 78
- Quality tier: bronze
- Public page: https://skillstore.pages.dev/skills/cjharmath-py-pydantic-patterns
- Manifest: https://skillstore.pages.dev/api/skills/cjharmath-py-pydantic-patterns/manifest

## Capabilities

- Explains Pydantic v1 to v2 API replacements for common model patterns.
- Shows field validators, model validators, and validation configuration patterns.
- Documents field constraints, reusable constrained types, and optional defaults.
- Covers discriminated unions for polymorphic request and response models.
- Describes serialization controls using model dumping, field serializers, and computed fields.
- Provides local grep commands for finding older Pydantic v1 patterns.

## Use Cases

- Migrate legacy Pydantic models: Replace v1 validators, config classes, and dump methods with Pydantic v2 equivalents.
- Design API request schemas: Create constrained input models with clear validation errors and OpenAPI-friendly field metadata.
- Review response serialization: Check model output, computed fields, aliases, and excluded fields before exposing API responses.

## Prompt Templates

### Create a simple model

```
Create a Pydantic v2 model for this request body. Include field constraints, helpful error messages, and serialization guidance.
```

### Convert v1 patterns

```
Review this Pydantic model for v1 patterns. List each required v2 change and explain the safer replacement.
```

### Validate related fields

```
Design Pydantic v2 validators for these related fields. Include when to use field validators versus model validators.
```

### Model polymorphic responses

```
Design a Pydantic v2 schema strategy for these polymorphic responses. Use discriminated unions and explain serialization tradeoffs.
```

## Limitations

- Does not execute migrations or rewrite files automatically.
- Does not cover every Pydantic v2 feature or plugin ecosystem.
- Examples are illustrative and may need imports or domain changes.
- Does not validate runtime behavior without project-specific tests.

## Best Practices

- Use Pydantic v2 APIs consistently when adding or changing models.
- Pair validator changes with tests for invalid, missing, and boundary values.
- Keep request, response, create, read, and update schemas separate when their fields differ.

## Anti Patterns

- Mixing v1 validators and v2 model configuration in the same schema.
- Returning ORM objects without enabling attribute-based model validation.
- Hiding serialization rules in ad hoc response code instead of model serializers.

## Security Audit

- Audited at: 2026-07-06T06:43:34.655\+00:00
- Summary: All static findings are false positives caused by Markdown code fences, inline-code formatting, or benign local grep examples in SKILL.md. No prompt injection, data exfiltration intent, destructive behavior, or unauthorized command execution was found.

## Stats

- Views: 208
- Downloads: 10
- Favorites: 0
- Popularity score: 0
