pydantic
Data validation using Python type hints
Python dicts from JSON APIs or config files are untyped โ you access fields by string keys, get None for missing values, and discover type mismatches at runtime rather than at the point of data entry. Pydantic was built to bring Python's type hint system to runtime data validation.
Define a Pydantic model the same way you'd define a dataclass, but with type annotations. When you instantiate it, Pydantic validates every field against its type, coerces compatible values (a string '42' becomes int 42), and raises a detailed ValidationError listing every problem at once โ not just the first one.
Install pydantic
pip install pydantic
import pydanticUse it when
Use Pydantic for parsing API responses, validating config files, defining request/response schemas in FastAPI, or any time you receive data from an external source and want guarantees about its shape and types.
Skip it when
Pydantic adds overhead on every model instantiation โ not ideal for tight inner loops parsing millions of small objects. For pure config files with no user-facing validation, dataclasses or simple TypedDicts may be lighter.
Ready to explore the full API?
See every class, function, and relationship in an interactive graph.