Pydantic v2
Data validation and serialization library for Python using type annotations. Pydantic v2 rewrote the validation core in Rust (pydantic-core), making it 5-50x faster than v1. Define Python classes with type annotations, get automatic validation, serialization (model_dump, model_dump_json), JSON schema generation, and detailed error messages. The standard data modeling library for Python — used by FastAPI, LangChain, and thousands of other libraries.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Local library — no network calls. Pydantic validation is a defense-in-depth layer — validate all external agent inputs. SecretStr type available for sensitive fields.
⚡ Reliability
Best When
You're building Python agent systems and need type-safe, validated data models with automatic JSON serialization — Pydantic v2 is the modern standard for Python data modeling.
Avoid When
You need very complex custom validation rules that fight Pydantic's model — attrs with validators may be more flexible for highly custom domain models.
Use Cases
- • Validate agent tool inputs and outputs with type-safe models — catch malformed agent data at boundaries with detailed field-level error messages
- • Serialize and deserialize agent data to/from JSON with model_dump_json() / model_validate_json() — fast Rust-powered JSON handling
- • Define agent message schemas for LLM structured output — use model_json_schema() to generate JSON Schema for API request/response contracts
- • Build typed configuration models for agent settings with validators, computed fields, and custom serializers
- • Use Pydantic discriminated unions for polymorphic agent event types — type-safe handling of multiple event schemas in a single field
Not For
- • Simple dict validation without type annotations — use cerberus or jsonschema for schema validation without Python class definitions
- • Runtime type checking of arbitrary Python code — Pydantic validates at model construction, not at arbitrary function call boundaries (use beartype for that)
- • Non-Python environments — Pydantic generates JSON Schema compatible with other validators, but the library itself is Python-only
Interface
Authentication
Local library — no external auth or network calls.
Pricing
MIT-licensed open source by Samuel Colvin and contributors.
Agent Metadata
Known Gotchas
- ⚠ Pydantic v2 is NOT backward compatible with v1 — many APIs changed (class Config → model_config, .dict() → .model_dump(), @validator → @field_validator); check migration guide before upgrading
- ⚠ model_validate() requires strict=True to prevent coercion — by default Pydantic coerces types ('42' → 42) which can mask agent data quality issues; use strict mode for exact type matching
- ⚠ Recursive models require model_rebuild() after all forward references resolve — models with self-referential types that use string annotations ('Node') must call model_rebuild() after class definition
- ⚠ model_dump(exclude_unset=True) vs model_dump(exclude_none=True) have different semantics — exclude_unset omits fields not provided at construction, not just None fields; agent code should verify which is needed
- ⚠ Field default factories (default_factory=list) create fresh instances per model — forgetting default_factory for mutable defaults causes all instances to share the same list, causing data corruption
- ⚠ Discriminated unions require the discriminator field to be present and correct — missing discriminator fields cause 'unable to extract tag' errors that require examining the input data structure carefully
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for Pydantic v2.
Scores are editorial opinions as of 2026-03-06.