Registry / serialization / pydantic

pydantic

JSON →
library2.13.4pypypi✓ verified 30d ago

The most widely used Python data validation library. Powers OpenAI SDK, Anthropic SDK, LangChain, FastAPI, LlamaIndex, and hundreds of other libraries. V2 released June 2023 — a near-complete rewrite in Rust via pydantic-core, 4-50x faster than V1. Current version is 2.12.5 (Mar 2026). V1 security fixes ended June 2024. V3 planned roughly annually.

pip install pydantic
INSTALL
IMPORT
SIG · PYDANTIC
P
pydantic
serializationpythonv2.13.4
Install
3.7s avg
Import
240ms
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.13.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.915 runs
installs and imports cleanly · install 0.0s · import 0.252s · 31.1MB
glibc
py 3.10–3.915 runs
installs and imports cleanly · install 3.7s · import 0.229s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BaseModel
✓ from pydantic import BaseModel, field_validator, model_validator class User(BaseModel): name: str age: int @field_validator('age') @classmethod def age_must_be_positive(cls, v): if v <= 0: raise ValueError('age must be positive') return v
✗ from pydantic import BaseModel, validator class User(BaseModel): name: str @validator('name') def validate_name(cls, v): return v
@validator is deprecated in V2, removed in V3. Use @field_validator. Must add @classmethod decorator in V2.
model_validator
✓ from pydantic import BaseModel, model_validator class User(BaseModel): name: str age: int @model_validator(mode='before') @classmethod def check_root(cls, values): return values
✗ from pydantic import BaseModel, root_validator class User(BaseModel): @root_validator(pre=True) def check_root(cls, values): return values
@root_validator deprecated in V2, removed in V3. Use @model_validator(mode='before') or mode='after'.
ConfigDict
✓ from pydantic import BaseModel, ConfigDict class User(BaseModel): model_config = ConfigDict(from_attributes=True) name: str
✗ from pydantic import BaseModel class User(BaseModel): class Config: orm_mode = True
Inner Config class deprecated in V2. Use model_config = ConfigDict(...). orm_mode renamed to from_attributes.
BaseSettings
✓ from pydantic_settings import BaseSettings class Settings(BaseSettings): api_key: str model_config = ConfigDict(env_file='.env')
✗ from pydantic import BaseSettings
BaseSettings moved to separate pydantic-settings package in V2. ImportError if using old import.

Pydantic V2 style model with field validator and config.

from pydantic import BaseModel, field_validator, ConfigDict class User(BaseModel): model_config = ConfigDict(from_attributes=True) name: str age: int @field_validator('age') @classmethod def age_positive(cls, v): assert v > 0, 'age must be positive' return v user = User(name='Alice', age=30) print(user.model_dump()) # not .dict() print(user.model_dump_json()) # not .json()
Debug
Known issues
breaking@validator decorator is deprecated in V2 and will be removed in V3. LLMs trained on pre-2023 data consistently generate @validator code. Raises PydanticDeprecatedSince20 warning.
fix
Replace @validator('field') with @field_validator('field') and add @classmethod decorator.
affects: >= 2.0
breaking@root_validator deprecated in V2, removed in V3. Replace with @model_validator(mode='before') or @model_validator(mode='after').
fix
@model_validator(mode='before') replaces @root_validator(pre=True). @model_validator(mode='after') replaces @root_validator(pre=False).
affects: >= 2.0
breakingInner class Config: is deprecated. Use model_config = ConfigDict(...) at class level.
fix
from pydantic import ConfigDict; model_config = ConfigDict(from_attributes=True)
affects: >= 2.0
breakingorm_mode = True renamed to from_attributes = True in ConfigDict.
fix
model_config = ConfigDict(from_attributes=True)
affects: >= 2.0
breaking.dict() method deprecated. Use .model_dump() instead. .json() deprecated, use .model_dump_json().
fix
user.model_dump() not user.dict(). user.model_dump_json() not user.json().
affects: >= 2.0
breakingparse_obj() and parse_raw() removed. Use model_validate() and model_validate_json() instead.
fix
User.model_validate(data) not User.parse_obj(data).
affects: >= 2.0
breakingBaseSettings moved to separate pydantic-settings package. 'from pydantic import BaseSettings' raises ImportError in V2.
fix
pip install pydantic-settings; from pydantic_settings import BaseSettings
affects: >= 2.0
breakingField(regex=...) renamed to Field(pattern=...) in V2.
fix
Field(pattern=r'^\d+$') not Field(regex=r'^\d+$')
affects: >= 2.0
gotchaV1 compat layer available via 'from pydantic.v1 import BaseModel' for gradual migration. But mixing V1 and V2 BaseModel in same codebase causes hard-to-debug errors.
fix
Migrate fully to V2. Use pydantic.v1 only as a temporary bridge.
affects: >= 2.0
gotchapydantic-core is a Rust extension. On unsupported platforms or Python versions, installation fails with no binary wheel available. Requires Python 3.8+.
fix
Ensure Python >= 3.8. Check pydantic-core wheel availability for your platform.
affects: >= 2.0
Errors
Common errors & fixes
AttributeError: 'BaseModel' object has no attribute 'dict'
In Pydantic V2, the `.dict()` method on `BaseModel` instances has been renamed and replaced by `.model_dump()` to avoid confusion with Python's built-in `dict()` function and to support new serialization features.
fix
Replace `.dict()` with `.model_dump()` to serialize models to a Python dictionary, or `.model_dump_json()` to get a JSON string.
PydanticUndefinedAnnotation: name 'YourType' is not defined
This error occurs when a type annotation refers to a class or type hint that has not yet been defined in the current scope, often due to forward references, circular imports, or simple typos.
fix
For forward references, use string literal annotations (e.g., `a: 'MyModel'`) and ensure `from __future__ import annotations` is imported for Python versions older than 3.9. If necessary, call `YourModel.model_rebuild()` after all related models are defined.
PydanticImportError: cannot import name 'validator' from 'pydantic'
Pydantic V2 introduced significant changes, including renaming and reorganizing modules and functions. The `@validator` decorator from V1 has been replaced by `@field_validator` and `@model_validator` in V2, requiring an update to import paths and usage.
fix
Update imports from `pydantic` to use the new validator decorators, e.g., `from pydantic import field_validator` or `from pydantic import model_validator`. Also, ensure other V1-specific imports or configurations are updated according to the V2 migration guide.
ValidationError: 1 validation error for ModelName field_name Input should be a valid integer [type=int_parsing,...]
This is a general validation error indicating that the input data provided for a specific field does not conform to its declared type or any defined constraints (e.g., an integer field received a non-integer string).
fix
Inspect the `loc` and `type` fields within the `ValidationError.errors()` output to identify the exact field and reason for the failure, then adjust the input data to match the expected type and constraints of the Pydantic model.
PydanticUserError: Field 'x' has 'regex' set but 'pattern' should be used instead
In Pydantic V2, several keyword arguments for the `Field` function were renamed or removed for consistency and clarity. Specifically, `regex` was replaced by `pattern` to specify regular expression constraints.
fix
Replace the deprecated `regex` argument in `Field()` with `pattern`, like `field_name: str = Field(pattern='your_regex_pattern')`. Review the V2 migration guide for other renamed `Field` arguments (e.g., `min_items` to `min_length`).
Upgrade
Version history
2.13.4latest on PyPI · released May 6, 2026
Audit
Dependencies
pydantic-corerequiredRust-based core engine. Installed automatically. Do not pin separately.
pydantic-settingsoptionalBaseSettings moved to separate package in V2. Required for settings/env var management.
email-validatoroptionalRequired for EmailStr type. Install via pydantic[email].
Agent activity
31 hits · last 30 days
node
24
Amazon
1
Resources