Skip to main content

Data validation using Python type hints

Project description

Pydantic

CI Coverage pypi CondaForge downloads versions license Pydantic v2

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 Logfire :fire:

We've recently 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 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)
#> 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.

Changelog

v2.10.2 (2024-11-25)

GitHub release

What's Changed

Fixes

  • Only evaluate FieldInfo annotations if required during schema building by @Viicos in #10769
  • Do not evaluate annotations for private fields by @Viicos in #10962
  • Support serialization as any for Secret types and Url types by @sydney-runkle in #10947
  • Fix type hint of Field.default to be compatible with Python 3.8 and 3.9 by @Viicos in #10972
  • Add hashing support for URL types by @sydney-runkle in #10975
  • Hide BaseModel.__replace__ definition from type checkers by @Viicos in 10979

v2.10.1 (2024-11-21)

GitHub release

What's Changed

Packaging

Fixes

v2.10.0 (2024-11-20)

The code released in v2.10.0 is practically identical to that of v2.10.0b2.

GitHub release

See the v2.10 release blog post for the highlights!

What's Changed

Packaging

New Features

Changes

Performance

  • Schema cleaning: skip unnecessary copies during schema walking by @Viicos in #10286
  • Refactor namespace logic for annotations evaluation by @Viicos in #10530
  • Improve email regexp on edge cases by @AlekseyLobanov in #10601
  • CoreMetadata refactor with an emphasis on documentation, schema build time performance, and reducing complexity by @sydney-runkle in #10675

Fixes

New Contributors

v2.10.0b2 (2024-11-13)

Pre-release, see the GitHub release for details.

v2.10.0b1 (2024-11-06)

Pre-release, see the GitHub release for details.

v2.9.2 (2024-09-17)

GitHub release

What's Changed

Fixes

v2.9.1 (2024-09-09)

GitHub release

What's Changed

Fixes

v2.9.0 (2024-09-05)

GitHub release

The code released in v2.9.0 is practically identical to that of v2.9.0b2.

What's Changed

Packaging

New Features

Changes

Performance

Minor Internal Improvements
  • ⚡️ Speed up multiple_of_validator() by 31% in pydantic/_internal/_validators.py by @misrasaurabh1 in #9839
  • ⚡️ Speed up ModelPrivateAttr.__set_name__() by 18% in pydantic/fields.py by @misrasaurabh1 in #9841
  • ⚡️ Speed up dataclass() by 7% in pydantic/dataclasses.py by @misrasaurabh1 in #9843
  • ⚡️ Speed up function _field_name_for_signature by 37% in pydantic/_internal/_signature.py by @misrasaurabh1 in #9951
  • ⚡️ Speed up method GenerateSchema._unpack_refs_defs by 26% in pydantic/_internal/_generate_schema.py by @misrasaurabh1 in #9949
  • ⚡️ Speed up function apply_each_item_validators by 100% in pydantic/_internal/_generate_schema.py by @misrasaurabh1 in #9950
  • ⚡️ Speed up method ConfigWrapper.core_config by 28% in pydantic/_internal/_config.py by @misrasaurabh1 in #9953

Fixes

New Contributors

pydantic

pydantic-core

v2.9.0b2 (2024-08-30)

Pre-release, see the GitHub release for details.

v2.9.0b1 (2024-08-26)

Pre-release, see the GitHub release for details.

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

  • Fix bug with JSON schema for sequence of discriminated union by @dmontagu in #7647
  • Fix schema references in discriminated unions by @adriangb in #7646
  • Fix json schema generation for recursive models by @adriangb in #7653
  • Fix models_json_schema for generic models by @adriangb in #7654
  • Fix xfailed test for generic model signatures by @adriangb in #7658

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 ValidationErrors 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.

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic-2.10.2.tar.gz (785.4 kB view details)

Uploaded Source

Built Distribution

pydantic-2.10.2-py3-none-any.whl (456.4 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-2.10.2.tar.gz.

File metadata

  • Download URL: pydantic-2.10.2.tar.gz
  • Upload date:
  • Size: 785.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pydantic-2.10.2.tar.gz
Algorithm Hash digest
SHA256 2bc2d7f17232e0841cbba4641e65ba1eb6fafb3a08de3a091ff3ce14a197c4fa
MD5 7b030a56f0ffafd7403a8a786c2987ca
BLAKE2b-256 4186a03390cb12cf64e2a8df07c267f3eb8d5035e0f9a04bb20fb79403d2a00e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic-2.10.2.tar.gz:

Publisher: ci.yml on pydantic/pydantic

Attestations:

File details

Details for the file pydantic-2.10.2-py3-none-any.whl.

File metadata

  • Download URL: pydantic-2.10.2-py3-none-any.whl
  • Upload date:
  • Size: 456.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pydantic-2.10.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cfb96e45951117c3024e6b67b25cdc33a3cb7b2fa62e239f7af1378358a1d99e
MD5 221c45e26e790bc79c0e8435b829a794
BLAKE2b-256 d574da832196702d0c56eb86b75bfa346db9238617e29b0b7ee3b8b4eccfe654

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic-2.10.2-py3-none-any.whl:

Publisher: ci.yml on pydantic/pydantic

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page