Pydantic
raw JSON → 2.12.5 verified Tue May 12 auth: no python install: verified quickstart: verified
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 Common errors
error AttributeError: 'BaseModel' object has no attribute 'dict' ↓
cause 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. error PydanticUndefinedAnnotation: name 'YourType' is not defined ↓
cause 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. error PydanticImportError: cannot import name 'validator' from 'pydantic' ↓
cause 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. error ValidationError: 1 validation error for ModelName field_name Input should be a valid integer [type=int_parsing,...] ↓
cause 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. error PydanticUserError: Field 'x' has 'regex' set but 'pattern' should be used instead ↓
cause 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). Warnings
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.
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).
breaking Inner class Config: is deprecated. Use model_config = ConfigDict(...) at class level. ↓
fix from pydantic import ConfigDict; model_config = ConfigDict(from_attributes=True)
breaking orm_mode = True renamed to from_attributes = True in ConfigDict. ↓
fix model_config = ConfigDict(from_attributes=True)
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().
breaking parse_obj() and parse_raw() removed. Use model_validate() and model_validate_json() instead. ↓
fix User.model_validate(data) not User.parse_obj(data).
breaking BaseSettings moved to separate pydantic-settings package. 'from pydantic import BaseSettings' raises ImportError in V2. ↓
fix pip install pydantic-settings; from pydantic_settings import BaseSettings
breaking Field(regex=...) renamed to Field(pattern=...) in V2. ↓
fix Field(pattern=r'^\d+$') not Field(regex=r'^\d+$')
gotcha V1 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.
gotcha pydantic-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.
Install
pip install 'pydantic[email]' pip install pydantic-settings Install compatibility verified last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) email - - 0.27s 31.3M
3.10 alpine (musl) pydantic - - 0.28s 27.7M
3.10 alpine (musl) pydantic-settings - - 0.28s 28.4M
3.10 slim (glibc) email - - 0.20s 31M
3.10 slim (glibc) pydantic - - 0.18s 27M
3.10 slim (glibc) pydantic-settings - - 0.19s 28M
3.11 alpine (musl) email - - 0.45s 34.4M
3.11 alpine (musl) pydantic - - 0.46s 30.3M
3.11 alpine (musl) pydantic-settings - - 0.45s 31.1M
3.11 slim (glibc) email - - 0.38s 34M
3.11 slim (glibc) pydantic - - 0.37s 30M
3.11 slim (glibc) pydantic-settings - - 0.37s 31M
3.12 alpine (musl) email - - 0.65s 26.0M
3.12 alpine (musl) pydantic - - 0.66s 22.0M
3.12 alpine (musl) pydantic-settings - - 0.63s 22.8M
3.12 slim (glibc) email - - 0.63s 26M
3.12 slim (glibc) pydantic - - 0.64s 22M
3.12 slim (glibc) pydantic-settings - - 0.59s 22M
3.13 alpine (musl) email - - 0.25s 25.6M
3.13 alpine (musl) pydantic - - 0.26s 21.6M
3.13 alpine (musl) pydantic-settings - - 0.26s 22.4M
3.13 slim (glibc) email - - 0.26s 25M
3.13 slim (glibc) pydantic - - 0.25s 21M
3.13 slim (glibc) pydantic-settings - - 0.26s 22M
3.9 alpine (musl) email - - 0.25s 30.5M
3.9 alpine (musl) pydantic - - 0.26s 27.2M
3.9 alpine (musl) pydantic-settings - - 0.24s 27.8M
3.9 slim (glibc) email - - 0.24s 30M
3.9 slim (glibc) pydantic - - 0.24s 27M
3.9 slim (glibc) pydantic-settings - - 0.24s 27M
Imports
- BaseModel wrong
from pydantic import BaseModel, validator class User(BaseModel): name: str @validator('name') def validate_name(cls, v): return vcorrectfrom 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 - model_validator wrong
from pydantic import BaseModel, root_validator class User(BaseModel): @root_validator(pre=True) def check_root(cls, values): return valuescorrectfrom pydantic import BaseModel, model_validator class User(BaseModel): name: str age: int @model_validator(mode='before') @classmethod def check_root(cls, values): return values - ConfigDict wrong
from pydantic import BaseModel class User(BaseModel): class Config: orm_mode = Truecorrectfrom pydantic import BaseModel, ConfigDict class User(BaseModel): model_config = ConfigDict(from_attributes=True) name: str - BaseSettings wrong
from pydantic import BaseSettingscorrectfrom pydantic_settings import BaseSettings class Settings(BaseSettings): api_key: str model_config = ConfigDict(env_file='.env')
Quickstart verified last tested: 2026-05-12
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()