Pydantic Validation
Data validation using Python type hints.
Fast and extensible, Pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.10+; validate it with Pydantic.
Pydantic Logfire 🔥
We’ve launched Pydantic Logfire to help you monitor your applications. Learn more
Pydantic V1.10 vs. V2
Pydantic V2 is a ground-up rewrite that offers many new features, performance improvements, and some breaking changes compared to Pydantic V1.
If you’re using Pydantic V1 you may want to look at the pydantic V1.10 Documentation or, 1.10.X-fixes git branch. Pydantic V2 also ships with the latest version of Pydantic V1 built in so that you can incrementally upgrade your code base and projects: from pydantic import v1 as pydantic_v1.
Help
See documentation for more details.
Installation
Install using pip install -U pydantic or conda install pydantic -c conda-forge. For more installation options to make Pydantic even faster, see the Install section in the documentation.
A Simple Example
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str = 'John Doe'
signup_ts: Optional[datetime] = None
friends: list[int] = []
external_data = {'id': '123', 'signup_ts': '2017-06-01 12:22', 'friends': [1, '2', b'3']}
user = User(**external_data)
print(user)
#> User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
print(user.id)
#> 123
Contributing
For guidance on setting up a development environment and how to make a contribution to Pydantic, see Contributing to Pydantic.
Reporting a Security Vulnerability
See our security policy.
Summary
Python data validation using type annotations (28.9k stars, MIT): BaseModel classes with standard-typing fields, rust-core (pydantic-core) validation in v2 (~5-50x faster than v1), JSON Schema generation, serialization, discriminated unions. 812M monthly pip downloads — FastAPI’s validation layer and Python’s de-facto schema standard.
Learnings
- The Python counterpart in the schema-validation family: where zod/valibot infer types FROM schemas, pydantic validates FROM types (annotations first, BaseModel second) — the inverse direction of the API. See Zod vs Valibot note’s cross-language section. The whole garden pipeline (frontmatter.py check, ocli, Hermes skills) rides on the same annotation-first idea.
Gotchas
- (none yet)
Hands-on
(in active use everywhere in the Python toolchain)