Pydantic is Python's standard data-validation library, maintained by Pydantic Services Inc. and creator Samuel Colvin. It ranks #7 among trending tech tags at roughly 10.1% usage share. It's trending because it's the validation engine baked into FastAPI, so as API-first Python backends have exploded in popularity, Pydantic has become an unavoidable part of nearly every modern Python service.
You define a data shape as a class with type-hinted fields, then Pydantic validates and coerces raw input (like a dict from a JSON request body) against it, raising a clear error on mismatch.
from pydantic import BaseModel, EmailStr, ValidationError
class User(BaseModel):
id: int
name: str
email: EmailStr
is_admin: bool = False
# raw input, e.g. from a request body
data = {"id": "42", "name": "Ava Chen", "email": "[email protected]"}
try:
user = User(**data)
print(user.id, type(user.id)) # 42 -- coerced from str
except ValidationError as e:
print(e.json())
Install it with pip (or add it to your FastAPI project, which already depends on it), then define your first model.
# install Pydantic
pip install pydantic
# run a quick script that uses it
python models.py