pydantic

Guides Pydantic data validation and serialization modeling in Python.

3|1|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/PALabs-v1/AI_friend --skill pydantic-palabs-v1
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pydantic
Source: https://github.com/PALabs-v1/AI_friend/tree/main/.claude/skills/pydantic
Command: npx skills add https://github.com/PALabs-v1/AI_friend --skill pydantic-palabs-v1

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pydantic, annotated_types, typing_extensions.

What problem does it solve? Complex data modeling with Pydantic involves subtle pitfalls—lost subclass fields during serialization, misapplied field metadata, broken forward references, and validator ordering issues—that produce silent bugs in APIs and data pipelines. ## Core Features & Use Cases - Field Metadata & Constraints: Correct usage of Field(), Annotated patterns, StringConstraints, and built-in constraints instead of custom validators. - Model Hierarchies: Discriminated unions, generics, and polymorphic serialization to avoid subclass serialization data loss. - Forward References & Aliases: Guidance on recursive type aliases, deferred annotation evaluation, and avoiding stringified annotations. - Use Case: When building a FastAPI endpoint with a polymorphic request body, apply discriminated unions so each subclass validates and serializes with its own fields intact. ## Quick Start Ask the AI to review or write a Pydantic model with constraints, validators, or a subclass hierarchy following these best practices.

Frequently Asked Questions about pydantic

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

FAQPage Schema
How do I add constraints to Pydantic model fields?

Use the Field() function or Annotated pattern with built-in constraints like gt or max_length, or annotated_types such as Gt. Prefer these over custom validators, and use StringConstraints for options like strip_whitespace or to_upper.

Why does Pydantic serialization drop subclass fields?

Pydantic serializes according to the declared type, not the runtime subclass, so fields defined only on the subclass are omitted. Use discriminated unions, generics, or polymorphic serialization to preserve subclass fields.

Should I use before or after validators in Pydantic?

Prefer after validators because the value is already coerced to the field's type. Before validators receive arbitrary input that may not even be a dict, making them more error-prone, especially for model validators.

Does Pydantic support recursive type aliases?

Yes, but quoted TypeAlias definitions generally cannot be evaluated by Pydantic. Use an explicit type statement on Python 3.12+ or TypeAliasType from typing_extensions so Pydantic can resolve the recursive reference.

When should I avoid using Pydantic models?

Avoid Pydantic for classes instantiated only within your own code, since it reduces flexibility with unsupported types and post-init changes. Use vanilla classes or standard dataclasses there, reserving Pydantic for untrusted external data.