logo

What is Pydantic?

Pydantic validates data using Python's type hints. You define what shape your data should have, and Pydantic enforces it.

Why care? External data - from APIs, files, user input - is messy. It might be the wrong type, missing fields, or have invalid values. Pydantic catches these problems early.

from pydantic import BaseModel

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

Now Pydantic will validate any data you pass:

user = User(name="Alice", age=30)  # Works
user = User(name="Alice", age="not a number")  # Error!

If the data doesn't match the schema, you get a clear error message explaining what's wrong.

Pydantic is the backbone of FastAPI and many other modern Python tools.

I cover Pydantic fundamentals in my Pydantic course.