Configuration & Data Formats

pydantic

The bouncer that checks every piece of data at the door before your program lets it in.

Install it: pip install pydantic

What does it do?

Pydantic looks at incoming data, a form submission, an API request, a config file, and checks it against a blueprint you’ve defined, like a customs officer checking a passport against a manifest. If someone sends a price as text instead of a number, or forgets a required field, Pydantic catches it immediately and explains exactly what’s wrong. You describe your data’s shape once using ordinary Python, and it turns that description into an active gatekeeper. It also automatically converts compatible types, so “42” becomes 42 without extra work.

See it in action

This defines what a “user” record should look like, then builds one from raw data, automatically turning the text “36” into the actual number 36 for the age.

from pydantic import BaseModel

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

user = User(name="Ada", age="36")
print(user.age, type(user.age))

Why would a non-developer care?

Every app you’ve used that gives you a specific, useful error message, such as “email must include an @”, instead of just crashing was probably doing something like this behind the scenes. It’s the difference between software that fails gracefully and software that fails mysteriously at 2am.

Real-world examples

Pydantic is the validation engine underneath FastAPI, one of the most popular Python web frameworks, and it has become a default dependency across much of the modern AI tooling ecosystem, including OpenAI’s own Python SDK, because structured, verified data matters even more when a language model is generating it. Without something like Pydantic, a single malformed request could silently corrupt a database instead of being rejected with a clear reason.

Who uses it

API developers, and increasingly AI engineers who need to force a language model’s freeform output into a strict, checkable structure.

How it compares to alternatives

Where marshmallow and Cerberus also validate data, Pydantic leans on Python’s native type hints instead of a separate schema language, which is why it became the backbone of FastAPI. It’s generally faster and more ergonomic for typed Python code, though marshmallow still has an edge for teams who prefer explicit, decoupled schemas.

Fun fact

Pydantic’s core validation logic was rewritten in Rust for version 2, making it dramatically faster while keeping the same friendly Python interface.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries