Skip to main content

pydantic-12-shims

A compatibility layer that lets you write Pydantic v2-style code that works with both Pydantic v1 and v2.

Problem

Pydantic v2 introduced significant API changes, outlined in their migration guide. Key changes include:

Pydantic v1 Pydantic v2
.parse_obj() .model_validate()
.dict() .model_dump()
.json() .model_dump_json()
class Config model_config = ConfigDict(...)
@validator @field_validator
@root_validator @model_validator

This library provides shims so you can write v2-style code that runs on both versions.

Why use this library?

In a heterogeneous environment where some projects use Pydantic v1 and others v2, this library lets you maintain a single codebase with v2-style syntax. You can incrementally migrate while still on pydantic<2 — ideally, the final change is just updating a pyproject.toml. Once all services use Pydantic v2, you can replace pydantic_12_shims imports with direct Pydantic v2 imports.

Pydantic v2 provides the pydantic.v1 module for this purpose, but pydantic.v1.BaseModel is not interoperable with native v2 BaseModel. For example, using a pydantic.v1 model as a field type in a native v2 model raises a TypeError:

# module1.py is still stuck on pydantic.v1:
from pydantic.v1 import BaseModel as V1BaseModel, Field

class MyLegacyModel(V1BaseModel):
    name: str = Field(default="legacy")


# module2.py wants to use native v2 models:
from pydantic import BaseModel
from module1 import MyLegacyModel

class ShinyNewModel(BaseModel):
    legacy_value: MyLegacyModel
    # TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given

This led major libraries like FastAPI to drop pydantic.v1 support entirely, making it an unviable migration path.

Installation

pip install pydantic-12-shims

Usage

Replace your Pydantic imports with imports from this library:

# Instead of:
# from pydantic import BaseModel, Field, field_validator, model_validator, ConfigDict

# Use:
from pydantic_12_shims import BaseModel, Field, field_validator, model_validator, ConfigDict

Then write your models using Pydantic v2 syntax:

from pydantic_12_shims import BaseModel, ConfigDict, Field, field_validator, model_validator

class User(BaseModel):
    model_config = ConfigDict(frozen=True, populate_by_name=True)

    name: str = Field(min_length=1)
    email: str
    age: int = Field(ge=0)

    @field_validator("email")
    @classmethod
    def validate_email(cls, v: str) -> str:
        if "@" not in v:
            raise ValueError("Invalid email")
        return v.lower()

    @model_validator(mode="after")
    def validate_model(self):
        # validation logic
        return self

# Use v2-style methods
user = User.model_validate({"name": "Alice", "email": "ALICE@example.com", "age": 30})
data = user.model_dump()
json_str = user.model_dump_json()

This code works identically whether Pydantic v1 or v2 is installed.

API Reference

Exports

  • BaseModel - Base class for models with v2-style methods
  • ConfigDict - Configuration dictionary (v2 style)
  • Field - Field definition with v2 parameter names
  • field_validator - Field validator decorator (v2 style)
  • model_validator - Model validator decorator (v2 style)
  • GenericModel - Base class for generic models
  • ValidationError - Validation exception
  • PrivateAttr - Private attribute marker
  • PYDANTIC1 / PYDANTIC2 - Booleans indicating which major version is installed

Validators

field_validator:

@field_validator("field_name", mode="before")  # or mode="after"
@classmethod
def validate_field(cls, v):
    return v

model_validator:

@model_validator(mode="before")  # receives dict
@classmethod
def validate_before(cls, values: dict) -> dict:
    return values

@model_validator(mode="after")  # receives model instance
def validate_after(self):
    return self

Note: support for validators is partial, e.g. the handler and info arguments are not yet supported.

Generic Models

For generic models, use GenericModel:

from typing import Generic, TypeVar
from pydantic_12_shims import GenericModel

T = TypeVar("T")

class Container(GenericModel, Generic[T]):
    value: T

# Parameterize the generic
IntContainer = Container[int]
instance = IntContainer(value=42)

Migrating off pydantic.v1

If your codebase uses Pydantic v2's pydantic.v1 compatibility module and you want to migrate away from it:

# Instead of:
# from pydantic.v1 import BaseModel

# Use:
from pydantic_12_shims.v1 import BaseModel

On Pydantic v2, pydantic_12_shims.v1 wraps pydantic.v1 models to be interoperable with native v2 BaseModel. Once every pydantic.v1 import has been replaced, set the environment variable TURN_PYDANTIC_V1_OFF=true to switch to native v2 models entirely — this lets you verify the migration works before removing the shim imports.

Version Detection

from pydantic_12_shims import PYDANTIC1, PYDANTIC2

if PYDANTIC1:
    # Pydantic v1 specific code
    pass

if PYDANTIC2:
    # Pydantic v2 specific code
    pass

License

See LICENSE file.

Release files for pydantic-12-shims 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-12-shims 0.0.1
File Size Uploaded
pydantic_12_shims-0.0.1.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-12-shims 0.0.1
File Interpreter ABI Platform
pydantic_12_shims-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / pydantic_12_shims-0.0.1.tar.gz

Download URL pydantic_12_shims-0.0.1.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d27ed4155f1760171b04be5e447846c03c7172d3eb280f293b5d5106917189d0
BLAKE2b-256 checksum
How to use checksums
f502df872fc3f47e298361dd18bb79baca38a6eea3e4091eae9a11d0b0904364
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 20, 2026.

Transparency log

Release files / pydantic_12_shims-0.0.1-py3-none-any.whl

Download URL pydantic_12_shims-0.0.1-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92a5d7589b2ec61ada3fddff666a00259a763abc345e4edc48f833f583e492c7
BLAKE2b-256 checksum
How to use checksums
a36cc9752c729b7c3ec726e377f32d059de23e71ec8aa8104081a3bd410d1364
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page