Pydantic
![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)
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.8+; validate it with Pydantic.
Pydantic Company :rocket:
We've started a company based on the principles that I believe have led to Pydantic's success.
Learn more from the Company Announcement.
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 List, 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)
print(user.id)
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.
Changelog
v2.8.2 (2024-07-03)
GitHub release
What's Changed
Fixes
- Fix issue with assertion caused by pluggable schema validator by @dmontagu in #9838
v2.8.1 (2024-07-03)
GitHub release
What's Changed
Packaging
Fixes
v2.8.0 (2024-07-01)
GitHub release
The code released in v2.8.0 is functionally identical to that of v2.8.0b1.
What's Changed
Packaging
New Features
Changes
- Add warning when "alias" is set in ignored
Annotated
field by @nix010 in #9170 - Support serialization of some serializable defaults in JSON schema by @sydney-runkle in #9624
- Relax type specification for
__validators__
values in create_model
by @sydney-runkle in #9697 - Breaking Change: Improve
smart
union matching logic by @sydney-runkle in pydantic/pydantic-core#1322
You can read more about our smart
union matching logic here. In some cases, if the old behavior
is desired, you can switch to left-to-right
mode and change the order of your Union
members.
Performance
Internal Improvements
- ⚡️ Speed up
_display_error_loc()
by 25% in pydantic/v1/error_wrappers.py
by @misrasaurabh1 in #9653 - ⚡️ Speed up
_get_all_json_refs()
by 34% in pydantic/json_schema.py
by @misrasaurabh1 in #9650 - ⚡️ Speed up
is_pydantic_dataclass()
by 41% in pydantic/dataclasses.py
by @misrasaurabh1 in #9652 - ⚡️ Speed up
to_snake()
by 27% in pydantic/alias_generators.py
by @misrasaurabh1 in #9747 - ⚡️ Speed up
unwrap_wrapped_function()
by 93% in pydantic/_internal/_decorators.py
by @misrasaurabh1 in #9727
Fixes
New Contributors
v2.8.0b1 (2024-06-27)
Pre-release, see the GitHub release for details.
v2.7.4 (2024-06-12)
Github release
What's Changed
Packaging
Fixes
- Specify
recursive_guard
as kwarg in FutureRef._evaluate
by @vfazio in #9612
v2.7.3 (2024-06-03)
GitHub release
What's Changed
Packaging
Fixes
v2.7.2 (2024-05-28)
GitHub release
What's Changed
Packaging
Fixes
v2.7.1 (2024-04-23)
GitHub release
What's Changed
Packaging
New Features
Changes
- Use field description for RootModel schema description when there is
…
by @LouisGobert in #9214
Fixes
New Contributors
v2.7.0 (2024-04-11)
GitHub release
The code released in v2.7.0 is practically identical to that of v2.7.0b1.
What's Changed
Packaging
New Features
Finalized in v2.7.0, rather than v2.7.0b1:
- Add support for field level number to str coercion option by @NeevCohen in #9137
- Update
warnings
parameter for serialization utilities to allow raising a warning by @Lance-Drane in #9166
Changes
Performance
Fixes
New Contributors
v2.7.0b1 (2024-04-03)
Pre-release, see the GitHub release for details.
v2.6.4 (2024-03-12)
GitHub release
What's Changed
Fixes
v2.6.3 (2024-02-27)
GitHub release
What's Changed
Packaging
Fixes
v2.6.2 (2024-02-23)
GitHub release
What's Changed
Packaging
Fixes
v2.6.1 (2024-02-05)
GitHub release
What's Changed
Packaging
Fixes
v2.6.0 (2024-01-23)
GitHub release
The code released in v2.6.0 is practically identical to that of v2.6.0b1.
What's Changed
Packaging
- Check for
email-validator
version >= 2.0 by @commonism in #6033 - Upgrade `ruff`` target version to Python 3.8 by @Elkiwa in #8341
- Update to
pydantic-extra-types==2.4.1
by @yezz123 in #8478 - Update to
pyright==1.1.345
by @Viicos in #8453 - Update pydantic-core from 2.14.6 to 2.16.1, significant changes from these updates are described below, full changelog here
New Features
Changes
Performance
Fixes
New Contributors
pydantic
pydantic-core
v2.6.0b1 (2024-01-19)
Pre-release, see the GitHub release for details.
v2.5.3 (2023-12-22)
GitHub release
What's Changed
Packaging
- uprev
pydantic-core
to 2.14.6
Fixes
v2.5.2 (2023-11-22)
GitHub release
What's Changed
Packaging
- uprev
pydantic-core
to 2.14.5
New Features
Fixes
v2.5.1 (2023-11-15)
GitHub release
What's Changed
Packaging
Fixes
v2.5.0 (2023-11-13)
GitHub release
The code released in v2.5.0 is functionally identical to that of v2.5.0b1.
What's Changed
Packaging
- Update pydantic-core from 2.10.1 to 2.14.1, significant changes from these updates are described below, full changelog here
- Update to
pyright==1.1.335
by @Viicos in #8075
New Features
Changes
- Significant Change: replace
ultra_strict
with new smart union implementation, the way unions are validated has changed significantly to improve performance and correctness, we have worked hard to absolutely minimise the number of cases where behaviour has changed, see the PR for details - by @davidhewitt in pydantic/pydantic-core#867 - Add support for instance method reassignment when
extra='allow'
by @sydney-runkle in #7683 - Support JSON schema generation for
Enum
types with no cases by @sydney-runkle in #7927 - Warn if a class inherits from
Generic
before BaseModel
by @alexmojaki in #7891
Performance
Fixes
New Contributors
pydantic
pydantic-core
v2.5.0b1 (2023-11-09)
Pre-release, see the GitHub release for details.
v2.4.2 (2023-09-27)
GitHub release
What's Changed
Fixes
New Contributors
v2.4.1 (2023-09-26)
GitHub release
What's Changed
Packaging
Fixes
v2.4.0 (2023-09-22)
GitHub release
What's Changed
Packaging
New Features
Changes
Performance
- Simplify flattening and inlining of
CoreSchema
by @adriangb in #7523 - Remove unused copies in
CoreSchema
walking by @adriangb in #7528 - Add caches for collecting definitions and invalid schemas from a CoreSchema by @adriangb in #7527
- Eagerly resolve discriminated unions and cache cases where we can't by @adriangb in #7529
- Replace
dict.get
and dict.setdefault
with more verbose versions in CoreSchema
building hot paths by @adriangb in #7536 - Cache invalid
CoreSchema
discovery by @adriangb in #7535 - Allow disabling
CoreSchema
validation for faster startup times by @adriangb in #7565
Fixes
- Fix config detection for
TypedDict
from grandparent classes by @dmontagu in #7272 - Fix hash function generation for frozen models with unusual MRO by @dmontagu in #7274
- Make
strict
config overridable in field for Path by @hramezani in #7281 - Use
ser_json_<timedelta|bytes>
on default in GenerateJsonSchema
by @Kludex in #7269 - Adding a check that alias is validated as an identifier for Python by @andree0 in #7319
- Raise an error when computed field overrides field by @sydney-runkle in #7346
- Fix applying
SkipValidation
to referenced schemas by @adriangb in #7381 - Enforce behavior of private attributes having double leading underscore by @lig in #7265
- Standardize
__get_pydantic_core_schema__
signature by @hramezani in #7415 - Fix generic dataclass fields mutation bug (when using
TypeAdapter
) by @sydney-runkle in #7435 - Fix
TypeError
on model_validator
in wrap
mode by @pmmmwh in #7496 - Improve enum error message by @hramezani in #7506
- Make
repr
work for instances that failed initialization when handling ValidationError
s by @dmontagu in #7439 - Fixed a regular expression denial of service issue by limiting whitespaces by @prodigysml in #7360
- Fix handling of
UUID
values having UUID.version=None
by @lig in #7566 - Fix
__iter__
returning private cached_property
info by @sydney-runkle in #7570 - Improvements to version info message by @samuelcolvin in #7594
New Contributors
v2.3.0 (2023-08-23)
GitHub release
v2.2.1 (2023-08-18)
GitHub release
v2.2.0 (2023-08-17)
GitHub release
v2.1.1 (2023-07-25)
GitHub release
v2.1.0 (2023-07-25)
GitHub release
v2.0.3 (2023-07-05)
GitHub release
v2.0.2 (2023-07-05)
GitHub release
- Fix bug where round-trip pickling/unpickling a
RootModel
would change the value of __dict__
, #6457 by @dmontagu - Allow single-item discriminated unions, #6405 by @dmontagu
- Fix issue with union parsing of enums, #6440 by @dmontagu
- Docs: Fixed
constr
documentation, renamed old regex
to new pattern
, #6452 by @miili - Change
GenerateJsonSchema.generate_definitions
signature, #6436 by @dmontagu
See the full changelog here
v2.0.1 (2023-07-04)
GitHub release
First patch release of Pydantic V2
- Extra fields added via
setattr
(i.e. m.some_extra_field = 'extra_value'
)
are added to .model_extra
if model_config
extra='allowed'
. Fixed #6333, #6365 by @aaraney - Automatically unpack JSON schema '$ref' for custom types, #6343 by @adriangb
- Fix tagged unions multiple processing in submodels, #6340 by @suharnikov
See the full changelog here
v2.0 (2023-06-30)
GitHub release
Pydantic V2 is here! :tada:
See this post for more details.
v2.0b3 (2023-06-16)
Third beta pre-release of Pydantic V2
See the full changelog here
v2.0b2 (2023-06-03)
Add from_attributes
runtime flag to TypeAdapter.validate_python
and BaseModel.model_validate
.
See the full changelog here
v2.0b1 (2023-06-01)
First beta pre-release of Pydantic V2
See the full changelog here
v2.0a4 (2023-05-05)
Fourth pre-release of Pydantic V2
See the full changelog here
v2.0a3 (2023-04-20)
Third pre-release of Pydantic V2
See the full changelog here
v2.0a2 (2023-04-12)
Second pre-release of Pydantic V2
See the full changelog here
v2.0a1 (2023-04-03)
First pre-release of Pydantic V2!
See this post for more details.
... see here for earlier changes.