Comparison Intermediate · 4 min read

Pydantic v1 vs v2 with LLM structured outputs

Quick answer
Use Pydantic v2 for LLM structured outputs due to its improved parsing speed, stricter validation, and native support for discriminated unions. Pydantic v1 remains functional but lacks these optimizations and modern features.

VERDICT

Use Pydantic v2 for LLM structured outputs because it offers faster parsing, better type safety, and enhanced model customization compared to Pydantic v1.
FeaturePydantic v1Pydantic v2Best for
Parsing speedSlower, Python-basedFaster, Rust-backed parsingHigh-performance LLM output validation
Validation strictnessFlexible but less strictStricter and more consistentReliable structured output enforcement
Discriminated unionsLimited supportNative and improved supportComplex LLM response schemas
Model customizationBasic config optionsEnhanced config and serializationAdvanced LLM output shaping

Key differences

Pydantic v2 introduces a Rust-based parser that significantly improves speed over v1. It enforces stricter validation rules, reducing silent errors in LLM structured outputs. Native support for discriminated unions in v2 simplifies modeling complex LLM responses. Additionally, v2 offers enhanced model customization and serialization options, making it better suited for robust AI integration.

Side-by-side example with Pydantic v1

This example shows how to parse a structured LLM JSON output using Pydantic v1. It defines a simple model and parses the LLM response string.

python
from pydantic import BaseModel
import json

class UserResponse(BaseModel):
    name: str
    age: int

llm_output = '{"name": "Alice", "age": 30}'
data = json.loads(llm_output)
user = UserResponse(**data)
print(user)
output
name='Alice' age=30

Equivalent example with Pydantic v2

Using Pydantic v2, the same structured output parsing benefits from faster validation and improved error messages. The model syntax is mostly compatible but leverages new features if needed.

python
from pydantic import BaseModel
import json

class UserResponse(BaseModel):
    name: str
    age: int

llm_output = '{"name": "Alice", "age": 30}'
data = json.loads(llm_output)
user = UserResponse.model_validate(data)
print(user)
output
name='Alice' age=30

When to use each

Use Pydantic v2 when you need high-performance parsing, strict validation, and support for complex LLM structured outputs like discriminated unions. Pydantic v1 is suitable for legacy projects or simpler use cases without performance constraints.

Use caseRecommended versionReason
High-throughput LLM output parsingPydantic v2Rust-based parser for speed
Complex nested LLM schemasPydantic v2Native discriminated unions support
Legacy codebasesPydantic v1Compatibility and stability
Simple validation tasksEitherBasic features suffice

Pricing and access

Pydantic is an open-source Python library with no cost. Both versions are freely available via PyPI. Use pip install pydantic for v1 and pip install pydantic==2.* for v2. No API keys or paid plans are required.

OptionFreePaidAPI access
Pydantic v1YesNoNo
Pydantic v2YesNoNo

Key Takeaways

  • Pydantic v2 is the best choice for validating structured LLM outputs due to speed and strictness.
  • Discriminated unions in v2 simplify handling complex LLM response schemas.
  • Pydantic v1 remains viable for legacy or simple use cases but lacks modern optimizations.
Verified 2026-04
Verify ↗