Glossary

Pydantic

Pydantic is a popular Python library for data validation and settings management using type annotations. You define a model by subclassing pydantic.BaseModel and annotating its fields with types; Pydantic automatically validates input data, converts compatible types, and raises detailed errors for invalid data. It is the validation engine behind FastAPI.

Pydantic V2 (released 2023) was rewritten with a Rust core (pydantic-core), making it 5–50x faster than V1. It supports all Python type annotation features — Optional, Union, generics, Literal, Annotated, nested models — and adds its own validators and serialisers. The model_validate() method parses dictionaries, JSON, or other sources into validated model instances.

Pydantic fills a gap between Python's dynamic typing and the need for validated, structured data. Where dataclasses provide convenience without validation, and type hints provide static analysis without runtime enforcement, Pydantic provides both: the structure of type-annotated classes with runtime validation and clear error messages. It is widely used for API request/ response models, configuration files, database record validation, and any context where untrusted data meets Python objects.

Related terms: Type Hint, Dataclass, FastAPI

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.