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.7+; 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. Learning 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)
#> 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.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.5.1.tar.gz (651.5 kB view details)

Uploaded Source

Built Distribution

pydantic-2.5.1-py3-none-any.whl (381.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic-2.5.1.tar.gz
  • Upload date:
  • Size: 651.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pydantic-2.5.1.tar.gz
Algorithm Hash digest
SHA256 0b8be5413c06aadfbe56f6dc1d45c9ed25fd43264414c571135c97dd77c2bedb
MD5 c75cd70dae08530d3d81534823332258
BLAKE2b-256 0b6ccebf0e87ee0f2496584e04079592f33610f1f9aaf3684cb3105f03969e2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic-2.5.1-py3-none-any.whl
  • Upload date:
  • Size: 381.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pydantic-2.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc5244a8939e0d9a68f1f1b5f550b2e1c879912033b1becbedb315accc75441b
MD5 150d216bb9651828dd4a11cea810058f
BLAKE2b-256 e22c9906b7abc337b0250a5634de5396e2f3cb1b837af0616424c2225a65aa80

See more details on using hashes here.

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