
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
pydantic
Advanced tools
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.9+; validate it with Pydantic.
We've recently launched Pydantic Logfire to help you monitor your applications. Learn more
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.
See documentation for more details.
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.
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
For guidance on setting up a development environment and how to make a contribution to Pydantic, see Contributing to Pydantic.
See our security policy.
This is the fifth 2.12 patch release, addressing an issue with the MISSING sentinel and providing several documentation improvements.
The next 2.13 minor release will be published in a couple weeks, and will include a new polymorphic serialization feature addressing the remaining unexpected changes to the serialize as any behavior.
model_construct() on a model with MISSING as a default value by @ornariece in #12522.This is the fourth 2.12 patch release, fixing more regressions, and reverting a change in the build() method
of the AnyUrl and Dsn types.
This patch release also fixes an issue with the serialization of IP address types, when serialize_as_any is used. The next patch release
will try to address the remaining issues with serialize as any behavior by introducing a new polymorphic serialization feature, that
should be used in most cases in place of serialize as any.
Fix issue with forward references in parent TypedDict classes by @Viicos in #12427.
This issue is only relevant on Python 3.14 and greater.
Exclude fields with exclude_if from JSON Schema required fields by @Viicos in #12430
Revert URL percent-encoding of credentials in the build() method
of the AnyUrl and Dsn types by @davidhewitt in
pydantic-core#1833.
This was initially considered as a bugfix, but caused regressions and as such was fully reverted. The next release will include an opt-in option to percent-encode components of the URL.
Add type inference for IP address types by @davidhewitt in pydantic-core#1868.
The 2.12 changes to the serialize_as_any behavior made it so that IP address types could not properly serialize to JSON.
Avoid getting default values from defaultdict by @davidhewitt in pydantic-core#1853.
This fixes a subtle regression in the validation behavior of the collections.defaultdict
type.
Fix issue with field serializers on nested typed dictionaries by @davidhewitt in pydantic-core#1879.
Add more pydantic-core builds for the three-threaded version of Python 3.14 by @davidhewitt in pydantic-core#1864.
This is the third 2.12 patch release, fixing issues related to the FieldInfo class, and reverting a change to the supported
after model validator function signatures.
FieldInfo.asdict() method, improve documentation around FieldInfo by @Viicos in #12411.
This also add back support for mutations on FieldInfo classes, that are reused as Annotated metadata. However, note that this is still
not a supported pattern. Instead, please refer to the added example in the documentation.The blog post section on changes was also updated to document the changes related to serialize_as_any.
pydantic-core version, as a corrupted CPython 3.10 manylinux2014_aarch64 wheel got uploaded (pydantic-core#1843).This is the first 2.12 patch release, addressing most (but not all yet) regressions from the initial 2.12.0 release.
None is converted as NoneType in Python 3.14 by @Viicos in #12370ValidationInfo for validation of default value by @Viicos in pydantic-core#1826MultiHostUrl builder by @willswire in pydantic-core#1829serialize_as_any serialization flag by @davidhewitt in pydantic-core#1829RootModel serialization issues by @davidhewitt in pydantic-core#1836This is the final 2.12 release. It features the work of 20 external contributors and provides useful new features, along with initial Python 3.14 support. Several minor changes (considered non-breaking changes according to our versioning policy) are also included in this release. Make sure to look into them before upgrading.
Note that Pydantic V1 is not compatible with Python 3.14 and greater.
Changes (see the alpha and beta releases for additional changes since 2.11):
extra parameter to the validate functions by @anvilpete in #12233exclude_computed_fields serialization option by @Viicos in #12334preverse_empty_path URL options by @Viicos in #12336union_format parameter to JSON Schema generation by @Viicos in #12147__qualname__ parameter for create_model by @Atry in #12001TypeAdapter by @Viicos in #12324Any for context type annotation in TypeAdapter by @inducer in #12279FieldInfo in pydantic.fields.__all__ by @Viicos in #12339validation_alias in @validate_call by @Viicos in #12340Any as context annotation in plugin API by @Viicos in #12341stacklevel in warnings when possible by @Viicos in #12342This is the first beta release of the upcoming 2.12 release.
exclude_if at the field level by @andresliszt in #12141ValidateAs annotation helper by @Viicos in #11942pydantic-core version is installed by @Viicos in #12196__getattr__() behavior on Pydantic models when a property raised an AttributeError and extra values are present by @raspuchin in #12106validate_assignment set by @Viicos in #12173ImportString JSON serialization for objects with a name attribute by @chr1sj0nes in #12219This is the first alpha release of the upcoming 2.12 release, which adds initial support for Python 3.14.
__pydantic_on_complete__() hook that is called once model is fully ready to be used by @DouweM in #11762Decimal type by @Dima-Bulavenko in #11987doc attribute on dataclass fields by @Viicos in #12077MISSING sentinel by @Viicos in #11883create_model() by @Viicos in #11714GenerateSchema class by @Viicos in #11733inspect.getsourcelines() for docstring extraction on Python 3.13 and greater by @Viicos in #11829FieldInfo creation implementation by @Viicos in #11898Secret covariant by @bluenote10 in #12008field_name in validator core schemas by @DouweM in #11761validate_assignment can be unpickled by @Viicos in #11769function-before schemas during schema gathering by @Viicos in #11801FieldInfo is complete after applying type variable map by @Viicos in #11855model_rebuild() by @Viicos in #11890FieldInfo by @Viicos in #11946SkipValidation by @ygsh0816 in #12002Field() default with validate_default set to True by @Viicos in #11988Field() function in dataclasses by @Viicos in #12051mkdocs-llmstxt to v0.2.0 by @Viicos in #11725pydantic-core to v2.35.1 by @Viicos in #11963FieldInfo by @Viicos in #11946FieldInfo is complete after applying type variable map by @Viicos in #11855model_rebuild() by @Viicos in #11890create_model() by @Viicos in #11714.
This change was backported as it was previously possible (although not meant to be supported)
to provide model_config as a field, which would make it possible to provide both configuration
and bases.function-before schemas during schema gathering by @Viicos in #11801pydantic-core to v2.33.1 by @Viicos in #11678__pydantic_private__ exists before setting private attributes by @Viicos in #11666FieldInfo._complete when using field from parent class by @Viicos in #11668'definitions-ref' schemas containing serialization schemas or metadata by @Viicos in #11644Pydantic v2.11 is a version strongly focused on build time performance of Pydantic models (and core schema generation in general). See the blog post for more details.
encoded_string() method to the URL types by @YassinNouh21 in #11580defer_build with @validate_call decorator by @Viicos in #11584@with_config decorator to be used with keyword arguments by @Viicos in #11608generate_arguments_schema() function by @Viicos in #11572check_pydantic_core_version() function by @Viicos in https://github.com/pydantic/pydantic/pull/11324greenlet development dependency by @Viicos in https://github.com/pydantic/pydantic/pull/11351typing-inspection library by @Viicos in https://github.com/pydantic/pydantic/pull/11479pydantic-core to v2.31.1 by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11526default_factory_takes_validated_data property to FieldInfo by @Viicos in https://github.com/pydantic/pydantic/pull/11034type[] by @Viicos in https://github.com/pydantic/pydantic/pull/11088create_model field definitions format by @Viicos in https://github.com/pydantic/pydantic/pull/11032model_fields and model_computed_fields on instances by @Viicos in https://github.com/pydantic/pydantic/pull/11169GenerateSchema class by @sydney-runkle in https://github.com/pydantic/pydantic/pull/10846get_type_ref by @Viicos in https://github.com/pydantic/pydantic/pull/10863pydantic-core core schema validation by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11271FieldInfo annotations if required during schema building by @Viicos in https://github.com/pydantic/pydantic/pull/10769__setattr__ performance of Pydantic models by caching setter functions by @MarkusSintonen in https://github.com/pydantic/pydantic/pull/10868_typing_extra module by @Viicos in https://github.com/pydantic/pydantic/pull/11255CoreConfig instance by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11384pydantic-core and thus use SchemaValidator and SchemaSerializer caching by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11402TypeAdapter instance repr by @sydney-runkle in https://github.com/pydantic/pydantic/pull/10872TypeAdapter by @Viicos in https://github.com/pydantic/pydantic/pull/10893Literal values if using PEP 695 type aliases by @Viicos in https://github.com/pydantic/pydantic/pull/11114__subclasscheck__ on ModelMetaclass to avoid memory leak and performance issues by @Viicos in https://github.com/pydantic/pydantic/pull/11116_extract_get_pydantic_json_schema() parameter by @Viicos in https://github.com/pydantic/pydantic/pull/11155Annotated form by @Viicos in https://github.com/pydantic/pydantic/pull/11109deprecated_instance_property warning by @Viicos in https://github.com/pydantic/pydantic/pull/11200WithJsonSchema schema to avoid sharing mutated data by @thejcannon in https://github.com/pydantic/pydantic/pull/11014CoreMetadata definition by @Viicos in https://github.com/pydantic/pydantic/pull/11216_Definitions class by @Viicos in https://github.com/pydantic/pydantic/pull/11208root type in the mypy plugin by @Viicos in https://github.com/pydantic/pydantic/pull/11212use_attribute_docstrings by @Viicos in https://github.com/pydantic/pydantic/pull/11246decimal_places_validator by @misrasaurabh1 in https://github.com/pydantic/pydantic/pull/11281validation_alias in the mypy plugin by @Viicos in https://github.com/pydantic/pydantic/pull/11295"examples" keys by @Viicos in https://github.com/pydantic/pydantic/pull/11305GenerateJsonSchema.literal_schema() implementation by @misrasaurabh1 in https://github.com/pydantic/pydantic/pull/11321ClickHouseDsn by @Maze21127 in https://github.com/pydantic/pydantic/pull/11319Decimal instances by @Viicos in https://github.com/pydantic/pydantic/pull/11350additionalProperties: True for arbitrary dictionary schemas by @austinyu in https://github.com/pydantic/pydantic/pull/11392fallback parameter in serialization methods by @Viicos in https://github.com/pydantic/pydantic/pull/11398Sequence types by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11435__get_validators__ on classes where __get_pydantic_core_schema__ is also defined by @tlambert03 in https://github.com/pydantic/pydantic/pull/11444validate_by_name to True when validate_by_alias is False by @sydney-runkle in https://github.com/pydantic/pydantic/pull/11503Any when synthesizing BaseSettings.__init__ signature in the mypy plugin by @Viicos in https://github.com/pydantic/pydantic/pull/11497ValueError on year zero by @davidhewitt in https://github.com/pydantic/pydantic-core/pull/1583dataclass InitVar shouldn't be required on serialization by @sydney-runkle in https://github.com/pydantic/pydantic-core/pull/1602Pydantic v2.11 is a version strongly focused on build time performance of Pydantic models (and core schema generation in general). This is another early alpha release, meant to collect early feedback from users having issues with core schema builds.
ruff from 0.9.2 to 0.9.5 by @Viicos in #11407pydantic-core to v2.29.0 by @mikeedjones in #11402CoreConfig instance by @sydney-runkle in #11384examples keys by @Viicos in #11366additionalProperties: True for arbitrary dictionary schemas by @austinyu in #11392fallback parameter in serialization methods by @Viicos in #11398Pydantic v2.11 is a version strongly focused on build time performance of Pydantic models (and core schema generation in general). This is an early alpha release, meant to collect early feedback from users having issues with core schema builds.
ruff to v0.9.0 by @sydney-runkle in #11254uv.lock deps update by @sydney-runkle in #11333check_pydantic_core_version() function by @Viicos in #11324greenlet development dependency by @Viicos in #11351pydantic-core to v2.28.0 by @Viicos in #11364default_factory_takes_validated_data property to FieldInfo by @Viicos in #11034type[] by @Viicos in #11088create_model field definitions format by @Viicos in #11032model_fields and model_computed_fields on instances by @Viicos in #11169GenerateSchema class by @sydney-runkle in #10846deque schema gen to GenerateSchema class by @sydney-runkle in #11239Mapping schema gen to GenerateSchema to complete removal of prepare_annotations_for_known_type workaround by @sydney-runkle in #11247pydantic-core core schema validation by @sydney-runkle in #11271FieldInfo annotations if required during schema building by @Viicos in #10769get_type_ref by @Viicos in #10863__setattr__ performance of Pydantic models by caching setter functions by @MarkusSintonen in #10868_typing_extra module by @Viicos in #11255_internal/_validators.py by @tkasuz in #10763TypeAdapter instance repr by @sydney-runkle in #10872Literal values if using PEP 695 type aliases by @Viicos in #11114__subclasscheck__ on ModelMetaclass to avoid memory leak and performance issues by @Viicos in #11116_extract_get_pydantic_json_schema() parameter by @Viicos in #11155Annotated form by @Viicos in #11109openapi-python-client check in issue creation for third-party failures, use main branch by @sydney-runkle in #11182deprecated_instance_property warning by @Viicos in #11200WithJsonSchema schema to avoid sharing mutated data by @thejcannon in #11014CoreMetadata definition by @Viicos in #11216_Definitions class by @Viicos in #11208root type in the mypy plugin by @Viicos in #11212use_attribute_docstrings by @Viicos in #11246decimal_places_validator by @misrasaurabh1 in #11281validation_alias in the mypy plugin by @Viicos in #11295GenerateJsonSchema.literal_schema() implementation by @misrasaurabh1 in #11321ClickHouseDsn by @Maze21127 in #11319Decimal instances by @Viicos in #11350ValueError on year zero by @davidhewitt in pydantic-core#1583'examples' keys by @Viicos in #11325pydantic-core to v2.27.2 by @davidhewitt in #11138AnyUrl objects by @alexprabhat99 in #11082len to _BaseUrl to avoid TypeError by @Kharianne in #11111defer_build is set on Pydantic dataclasses by @Viicos in #10984dict core schema keys by @Viicos in #10989PlainSerializer and WrapSerializer functions by @Viicos in #11008default_factory_takes_validated_data property to FieldInfo by @Viicos in #11034serialization mode by @sydney-runkle in #11035Secret types and Url types by @sydney-runkle in #10947Field.default to be compatible with Python 3.8 and 3.9 by @Viicos in #10972BaseModel.__replace__ definition from type checkers by @Viicos in #10979pydantic-core version to v2.27.1 by @sydney-runkle in #10938TypeAdapter by @Viicos in #10893default_factory utils by @sydney-runkle in #10909model_fields and model_computed_fields by @sydney-runkle in #10911dataclasses by @sydney-runkle in #10928globals of the function when evaluating the return type of serializers and computed_fields by @Viicos in #10929'' by @sydney-runkle in #10936python mode serialization for complex inference by @sydney-runkle in pydantic-core#1549The code released in v2.10.0 is practically identical to that of v2.10.0b2.
See the v2.10 release blog post for the highlights!
pydantic-core to v2.27.0 by @sydney-runkle in #10825fractions.Fraction by @sydney-runkle in #10318Hashable for json validation by @sydney-runkle in #10324SocketPath type for linux systems by @theunkn0wn1 in #10378examples by @sydney-runkle in #10417defer_build for Pydantic dataclasses by @Viicos in #10313TypedDict to type hint variadic keyword arguments with @validate_call by @Viicos in #10416protected_namespaces by @sydney-runkle in #10522propertyNames in JSON schema by @FlorianSW in #10478__replace__ protocol for Python 3.13+ support by @sydney-runkle in #10596sort method for JSON schema generation by @sydney-runkle in #10595@validate_call callable argument by @kc0506 in #10627experimental_allow_partial support by @samuelcolvin in #10748ValidationError and PydanticCustomError by @Youssefares in pydantic/pydantic-core#1413trailing-strings support to experimental_allow_partial by @sydney-runkle in #10825rebuild() method for TypeAdapter and simplify defer_build patterns by @sydney-runkle in #10537TypeAdapter instance repr by @sydney-runkle in #10872SchemaGenerator until interface is more stable by @sydney-runkle in #10303defer_build on TypeAdapters, removing experimental flag by @sydney-runkle in #10329mro of generic subclass by @kc0506 in #10100b64decode and b64encode for Base64Bytes type by @sydney-runkle in #10486@dataclass decorator and with the __pydantic_config__ attribute by @sydney-runkle in #10406Ellipsis (...) with Field by @Viicos in #10661Literals and Enums by @Viicos in #10692Any or Never when replacing type variables by @Viicos in #10338base64 bytes by @bschoenmaeckers in pydantic/pydantic-core#1448CoreMetadata refactor with an emphasis on documentation, schema build time performance, and reducing complexity by @sydney-runkle in #10675computed_field with field_serializer by @nix010 in #10390Predicate issue in v2.9.0 by @sydney-runkle in #10321annotated-types bound by @sydney-runkle in #10327tzdata install requirement into optional timezone dependency by @jakob-keller in #10331namedtuple core schemas by @Viicos in #10337IncEx type alias definition by @Viicos in #10339ModelMetaclass.mro by @Viicos in #10372computed_fields by @Viicos in #10391inspect.iscoroutinefunction works on coroutines decorated with @validate_call by @MovisLi in #10374NameError when using validate_call with PEP 695 on a class by @kc0506 in #10380ZoneInfo with various invalid types by @sydney-runkle in #10408PydanticUserError on empty model_config with annotations by @cdwilson in #10412_IncEx type alias, only allow True by @Viicos in #10414PlainValidator by @Viicos in #10427json_schema_input_type by @Viicos in #10439Representation by @Viicos in #10480max_digits and decimal_places) by @sydney-runkle in #10506__pydantic_core_schema__ from the current class during schema generation by @Viicos in #10518stacklevel on deprecation warnings for BaseModel by @sydney-runkle in #10520stacklevel in BaseModel.__init__ by @Viicos in #10526ConfigWrapper.core_config to take the title directly by @Viicos in #10562mode='python' by @sydney-runkle in #10594Base64Etc types by @sydney-runkle in #10584validate_call ignoring Field in Annotated by @kc0506 in #10610Self is invalid by @kc0506 in #10609core_schema.InvalidSchema instead of metadata injection + checks by @sydney-runkle in #10523type with typing.Self and type aliases by @kc0506 in #10621Field and PrivateAttr functions by @Viicos in #10651mypy plugin implementation by @Viicos in #10669typing_extensions variant of TypeAliasType by @Daraan in #10713BaseModel.model_copy() by @Viicos in #10751isinstance behavior for urls by @sydney-runkle in #10766cached_property can be set on Pydantic models by @Viicos in #10774host_required for URLs by @Viicos in pydantic/pydantic-core#1488coerce_numbers_to_str enabled and string has invalid Unicode character by @andrey-berenda in pydantic/pydantic-core#1515complex values in Enums by @changhc in pydantic/pydantic-core#1524_typing_extra module by @Viicos in #10725bytearray to TypeAdapter.validate_json signature by @samuelcolvin in #10802Field by @Viicos in #10816validate_call by @sydney-runkle in #10807IncEx type alias to be compatible with mypy by @Viicos in #10813__signature__ a lazy property, do not deepcopy defaults by @Viicos in #10818__signature__ lazy for dataclasses, too by @sydney-runkle in #10832AnyUrl to preserve behavior from v2.9 by @sydney-runkle in #10856Pre-release, see the GitHub release for details.
Pre-release, see the GitHub release for details.
... see here for earlier changes.
FAQs
Data validation using Python type hints
We found that pydantic demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.