pydantic-core
pydantic-core is the Rust-powered engine that underpins all validation and serialization in Pydantic V2+. Written in Rust via PyO3, it exposes SchemaValidator, SchemaSerializer, ValidationError, and the core_schema builder API to Python. End-users should interact with it only through the higher-level `pydantic` package; direct use is intended for library authors implementing custom types via __get_pydantic_core_schema__. The library releases in lockstep with pydantic — each pydantic-core version is pinned to exactly one pydantic version and does not follow SemVer independently. Current version is 2.44.0, released frequently (multiple times per month) alongside pydantic.
Warnings
- breaking pydantic-core does NOT follow SemVer. Each pydantic-core version is pinned to exactly one pydantic version. Never pin or upgrade pydantic-core independently of pydantic — doing so will cause immediate ImportError or runtime crashes.
- breaking The internal CoreSchema format (the dict structure produced by core_schema.*) is NOT considered stable API and may change between minor pydantic-core releases. Hardcoded CoreSchema dicts (without using core_schema builders) will silently break.
- breaking On platforms without a pre-built binary wheel (e.g. ARM musl/Alpine, exotic BSDs, very old macOS), pip falls back to building from source and requires a compatible Rust toolchain (rustc + cargo). The build will fail with 'can't find Rust compiler' or Cargo compile errors if Rust is absent or too old.
- breaking Deploying to cross-compiled environments (e.g. AWS Lambda with --platform manylinux, Docker buildx for arm64) and building on a different host OS/arch causes 'No module named pydantic_core._pydantic_core' at runtime because the .so extension does not match the target platform ABI.
- breaking Pydantic V1 is not compatible with Python 3.14 or greater. pydantic-core (V2) is required for Python 3.14+ support.
- gotcha validate_json() on SchemaValidator is significantly faster than calling json.loads() followed by validate_python() because it avoids creating intermediate Python objects. Using validate_python(json.loads(data)) is a common performance mistake.
- gotcha Importing from pydantic_core._pydantic_core (the private Rust extension module) instead of pydantic_core is unsupported. The private module's API surface can change without notice.
Install
-
pip install pydantic-core -
pip install pydantic
Imports
- SchemaValidator
from pydantic_core import SchemaValidator
- SchemaSerializer
from pydantic_core import SchemaSerializer
- ValidationError
from pydantic_core import ValidationError
- core_schema
from pydantic_core import core_schema
- PydanticCustomError
from pydantic_core import PydanticCustomError
- PydanticSerializationUnexpectedValue
from pydantic_core import PydanticSerializationUnexpectedValue
- CoreSchema (type alias)
from pydantic_core import CoreSchema
Quickstart
from pydantic_core import SchemaValidator, ValidationError, core_schema
# Build a schema using the core_schema helpers
schema = core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(core_schema.str_schema()),
'age': core_schema.typed_dict_field(
core_schema.int_schema(ge=18)
),
'is_developer': core_schema.typed_dict_field(
core_schema.with_default_schema(
core_schema.bool_schema(), default=True
),
required=False,
),
}
)
v = SchemaValidator(schema)
# Validate a Python dict
result = v.validate_python({'name': 'Alice', 'age': 30})
print(result) # {'name': 'Alice', 'age': 30, 'is_developer': True}
# Validate JSON bytes directly (faster than json.loads + validate_python)
result_json = v.validate_json('{"name": "Bob", "age": 25}')
print(result_json)
# Catch validation errors
try:
v.validate_python({'name': 'Eve', 'age': 15})
except ValidationError as e:
print(e)
# 1 validation error for typed-dict
# age
# Input should be greater than or equal to 18 [type=greater_than_equal, ...]