pydantic_core public API members
The pydantic_core module exports the following public API members: SchemaValidator, SchemaSerializer, ValidationError, ErrorDetails, InitErrorDetails, SchemaError, PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticUseDefault, PydanticSerializationError, PydanticSerializationUnexpectedValue, Url, MultiHostUrl, MultiHostHost, ArgsKwargs, Some, TzInfo, to_json, from_json, to_jsonable_python, list_all_errors, ErrorTypeInfo, and __version__.
pydantic_core.core_schema module
The pydantic_core.core_schema module contains the core schema definitions used by Pydantic for validation and serialization. This module is part of pydantic_core, which is the underlying validation engine for Pydantic.
pydantic-core provides core validation and serialization
pydantic-core is a package that provides the core functionality for pydantic validation and serialization. Users should not use pydantic-core directly; instead they should use pydantic, which in turn uses pydantic-core.
pydantic-core is 17x faster than pydantic V1
pydantic-core is currently around 17x faster than pydantic V1.
SchemaValidator direct usage example
The following example demonstrates direct usage of pydantic-core's SchemaValidator class:
from pydantic_core import SchemaValidator, ValidationError
v = SchemaValidator(
{
'type': 'typed-dict',
'fields': {
'name': {
'type': 'typed-dict-field',
'schema': {
'type': 'str',
},
},
'age': {
'type': 'typed-dict-field',
'schema': {
'type': 'int',
'ge': 18,
},
},
'is_developer': {
'type': 'typed-dict-field',
'schema': {
'type': 'default',
'schema': {'type': 'bool'},
'default': True,
},
},
},
}
)
r1 = v.validate_python({'name': 'Samuel', 'age': 35})
assert r1 == {'name': 'Samuel', 'age': 35, 'is_developer': True}
# pydantic-core can also validate JSON directly
r2 = v.validate_json('{"name": "Samuel", "age": 35}')
assert r1 == r2
try:
v.validate_python({'name': 'Samuel', 'age': 11})
except ValidationError as e:
print(e)
SchemaValidator methods validate_python and validate_json
SchemaValidator has two validation methods: validate_python() for validating Python objects and validate_json() for validating JSON directly.
pydantic_core.to_json function
The pydantic_core.to_json function is available for JSON serialization.
pydantic_core.from_json supports partial JSON parsing
The pydantic_core.from_json function accepts an allow_partial parameter. When allow_partial=True, it can deserialize incomplete JSON data, returning the portion that was successfully parsed. When allow_partial=False (the default), parsing errors occur on incomplete JSON.
Partial JSON parsing with from_json example
from_json can parse incomplete JSON when allow_partial=True. Example: from_json('["aa", "bb", "c', allow_partial=True) returns ['aa', 'bb']. This works with both arrays and dictionaries.
String caching details in JSON parser
String caching uses a fully associative cache with size 16,384. Only strings where len(string) < 64 are cached. String caching improves performance but increases memory usage slightly. There is overhead to cache lookup, so it may be worth disabling with cache_strings=False if very few repeated strings are in the data.
Pydantic v2.5.0+ uses jiter JSON parser
Starting in v2.5.0, Pydantic uses jiter, a fast and iterable JSON parser, instead of serde. The jiter parser is almost entirely compatible with serde and provides modest performance improvements. It supports deserialization of inf and NaN values.