Pydantic V3 – Complete Tutorial & Guide (2026)

Master Pydantic V3 – the most powerful data validation & serialization library for Python in 2026.

Why Pydantic V3 in 2026?

  • Used in FastAPI, LangChain, Typer, DataDog, and most modern APIs
  • 10–30× faster than V1/V2 in many cases
  • Native support for Python 3.12/3.14 typing features
  • Better error messages & developer experience

1. Installation


# Recommended (with email validator & strict types)
pip install "pydantic[email,strict]"

# Or minimal
pip install pydantic
            

2. Basics – Models & Fields


from pydantic import BaseModel, Field
from datetime import date
from typing import Optional

class User(BaseModel):
    id: int = Field(..., gt=0, description="Unique user ID")
    name: str = Field(min_length=2, max_length=50)
    email: str
    is_active: bool = True
    birth_date: Optional[date] = None

    model_config = {
        "str_strip_whitespace": True,
        "frozen": False,
    }

user = User(id=1, name="Alice", email="alice@example.com")
print(user.model_dump())
            

3. Validation & Custom Validators


from pydantic import BaseModel, field_validator, ValidationError

class User(BaseModel):
    password: str

    @field_validator("password")
    @classmethod
    def validate_password(cls, v: str) -> str:
        if len(v) < 8:
            raise ValueError("Password must be at least 8 characters")
        if not any(c.isupper() for c in v):
            raise ValueError("Password must contain uppercase letter")
        return v

try:
    User(password="weak")
except ValidationError as e:
    print(e.errors())
            

4. New & Powerful V3 Features (2026)

  • TypeIs / TypeGuard – better type narrowing
  • Strict mode – no coercion
  • Custom types with Annotated + AfterValidator
  • model_validate vs model_validate_json
  • @computed_field decorator

from typing import Annotated
from pydantic import AfterValidator, BaseModel

def strip_whitespace(v: str) -> str:
    return v.strip()

Username = Annotated[str, AfterValidator(strip_whitespace)]

class User(BaseModel):
    username: Username
            

5. Pydantic V3 with FastAPI


from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float = Field(gt=0)

@app.post("/items/")
async def create_item(item: Item):
    return item
            

Automatic validation, OpenAPI docs, and serialization — all powered by Pydantic V3.

6. Migration from V1/V2 to V3

  • Use model_config instead of class Config
  • Replace orm_mode with from_attributes=True
  • Update validators to @field_validator
  • Enable strict mode for safety

Ready to use Pydantic V3 in your project?

Explore All Tools →