{"id":140,"library":"pydantic","title":"Pydantic","description":"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.","status":"active","version":"2.12.5","language":"python","source_language":"en","source_url":"https://github.com/pydantic/pydantic","tags":["pydantic","validation","python","data-models","fastapi","llm","type-hints"],"install":[{"cmd":"pip install pydantic","lang":"bash","label":"Python (core, V2)"},{"cmd":"pip install 'pydantic[email]'","lang":"bash","label":"Python (with email validation)"},{"cmd":"pip install pydantic-settings","lang":"bash","label":"Python (settings management — separate package in V2)"}],"dependencies":[{"reason":"Rust-based core engine. Installed automatically. Do not pin separately.","package":"pydantic-core","optional":false},{"reason":"BaseSettings moved to separate package in V2. Required for settings/env var management.","package":"pydantic-settings","optional":true},{"reason":"Required for EmailStr type. Install via pydantic[email].","package":"email-validator","optional":true}],"imports":[{"note":"@validator is deprecated in V2, removed in V3. Use @field_validator. Must add @classmethod decorator in V2.","wrong":"from pydantic import BaseModel, validator\n\nclass User(BaseModel):\n    name: str\n\n    @validator('name')\n    def validate_name(cls, v):\n        return v","symbol":"BaseModel","correct":"from pydantic import BaseModel, field_validator, model_validator\n\nclass User(BaseModel):\n    name: str\n    age: int\n\n    @field_validator('age')\n    @classmethod\n    def age_must_be_positive(cls, v):\n        if v <= 0:\n            raise ValueError('age must be positive')\n        return v"},{"note":"@root_validator deprecated in V2, removed in V3. Use @model_validator(mode='before') or mode='after'.","wrong":"from pydantic import BaseModel, root_validator\n\nclass User(BaseModel):\n    @root_validator(pre=True)\n    def check_root(cls, values):\n        return values","symbol":"model_validator","correct":"from pydantic import BaseModel, model_validator\n\nclass User(BaseModel):\n    name: str\n    age: int\n\n    @model_validator(mode='before')\n    @classmethod\n    def check_root(cls, values):\n        return values"},{"note":"Inner Config class deprecated in V2. Use model_config = ConfigDict(...). orm_mode renamed to from_attributes.","wrong":"from pydantic import BaseModel\n\nclass User(BaseModel):\n    class Config:\n        orm_mode = True","symbol":"ConfigDict","correct":"from pydantic import BaseModel, ConfigDict\n\nclass User(BaseModel):\n    model_config = ConfigDict(from_attributes=True)\n    name: str"},{"note":"BaseSettings moved to separate pydantic-settings package in V2. ImportError if using old import.","wrong":"from pydantic import BaseSettings","symbol":"BaseSettings","correct":"from pydantic_settings import BaseSettings\n\nclass Settings(BaseSettings):\n    api_key: str\n    model_config = ConfigDict(env_file='.env')"}],"quickstart":{"code":"from pydantic import BaseModel, field_validator, ConfigDict\n\nclass User(BaseModel):\n    model_config = ConfigDict(from_attributes=True)\n\n    name: str\n    age: int\n\n    @field_validator('age')\n    @classmethod\n    def age_positive(cls, v):\n        assert v > 0, 'age must be positive'\n        return v\n\nuser = User(name='Alice', age=30)\nprint(user.model_dump())  # not .dict()\nprint(user.model_dump_json())  # not .json()","lang":"python","description":"Pydantic V2 style model with field validator and config."},"warnings":[{"fix":"Replace @validator('field') with @field_validator('field') and add @classmethod decorator.","message":"@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.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"@model_validator(mode='before') replaces @root_validator(pre=True). @model_validator(mode='after') replaces @root_validator(pre=False).","message":"@root_validator deprecated in V2, removed in V3. Replace with @model_validator(mode='before') or @model_validator(mode='after').","severity":"breaking","affected_versions":">= 2.0"},{"fix":"from pydantic import ConfigDict; model_config = ConfigDict(from_attributes=True)","message":"Inner class Config: is deprecated. Use model_config = ConfigDict(...) at class level.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"model_config = ConfigDict(from_attributes=True)","message":"orm_mode = True renamed to from_attributes = True in ConfigDict.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"user.model_dump() not user.dict(). user.model_dump_json() not user.json().","message":".dict() method deprecated. Use .model_dump() instead. .json() deprecated, use .model_dump_json().","severity":"breaking","affected_versions":">= 2.0"},{"fix":"User.model_validate(data) not User.parse_obj(data).","message":"parse_obj() and parse_raw() removed. Use model_validate() and model_validate_json() instead.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"pip install pydantic-settings; from pydantic_settings import BaseSettings","message":"BaseSettings moved to separate pydantic-settings package. 'from pydantic import BaseSettings' raises ImportError in V2.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Field(pattern=r'^\\d+$') not Field(regex=r'^\\d+$')","message":"Field(regex=...) renamed to Field(pattern=...) in V2.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Migrate fully to V2. Use pydantic.v1 only as a temporary bridge.","message":"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.","severity":"gotcha","affected_versions":">= 2.0"},{"fix":"Ensure Python >= 3.8. Check pydantic-core wheel availability for your platform.","message":"pydantic-core is a Rust extension. On unsupported platforms or Python versions, installation fails with no binary wheel available. Requires Python 3.8+.","severity":"gotcha","affected_versions":">= 2.0"}],"env_vars":null,"search_vec":"'-50':39 '2.12.5':47 '2023':26 '2024':55 '2026':49 '4':38 'annual':59 'anthrop':13 'complet':30 'core':37 'current':44 'data':7,64 'data-model':63 'end':53 'fastapi':16,66 'faster':41 'fix':52 'hint':70 'hundr':19 'june':25,54 'langchain':15 'librari':9,22 'llamaindex':17 'llm':67 'mar':48 'model':65 'near':29 'near-complet':28 'openai':11 'plan':57 'power':10 'pydant':1,36,60 'pydantic-cor':35 'python':6,62 'releas':24 'rewrit':31 'rough':58 'rust':33 'sdk':12,14 'secur':51 'type':69 'type-hint':68 'use':5 'v1':43,50 'v2':23 'v3':56 'valid':8,61 'version':45 'via':34 'wide':4 'x':40","created_at":"2026-03-24T03:55:30.640848+00:00","updated_at":"2026-04-17T14:50:57.739799+00:00","problems":[{"fix":"Replace `.dict()` with `.model_dump()` to serialize models to a Python dictionary, or `.model_dump_json()` to get a JSON string.","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.","error":"AttributeError: 'BaseModel' object has no attribute 'dict'"},{"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.","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.","error":"PydanticUndefinedAnnotation: name 'YourType' is not defined"},{"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.","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.","error":"PydanticImportError: cannot import name 'validator' from 'pydantic'"},{"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.","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).","error":"ValidationError: 1 validation error for ModelName field_name Input should be a valid integer [type=int_parsing,...]"},{"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`).","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.","error":"PydanticUserError: Field 'x' has 'regex' set but 'pattern' should be used instead"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"2.13.4","cli_name":"","cli_version":null,"type":"library","homepage":"https://docs.pydantic.dev","github":"https://github.com/sponsors/samuelcolvin","docs":"https://docs.pydantic.dev","changelog":"https://docs.pydantic.dev/latest/changelog/","pypi":"https://pypi.org/project/pydantic/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","web-framework","database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-27","last_verified":"2026-08-26","next_check":"2026-07-27","install_tag":"verified"}}