Skip to main content

pydantic-compat

GitHub PyPI Python Version CI codecov

Motivation

Pydantic 2 was a major release that completely changed the pydantic API.

For applications, this is not a big deal, as they can pin to whatever version of pydantic they need. But for libraries that want to exist in a broader environment, pinning to a specific version of pydantic is not always an option (as it limits the ability to co-exist with other libraries).

This package provides (unofficial) compatibility mixins and function adaptors for pydantic v1-v2 cross compatibility. It allows you to use either v1 or v2 API names, regardless of the pydantic version installed. (Prefer using v2 names when possible)

The API conversion is not exhaustive, but suffices for many of the use cases I have come across. I will be using it in:

Feel free to open an issue or PR if you find it useful, but lacking features you need.

What does it do?

Not much! 😂

Mostly serves to translate names from one API to another. While pydantic2 does offer deprecated access to the v1 API, if you explicitly wish to support pydantic1 without your users seeing deprecation warnings, then you need to do a lot of name adaptation depending on the runtime pydantic version. This package does that for you.

It does not do any significantly complex translation of API logic. For custom types, you will still likely need to add class methods to support both versions of pydantic.

It also does not prevent you from needing to know a what's changing under the hood in pydantic 2. You should be running tests on both versions of pydantic to ensure your library works as expected. This library just makes it much easier to support both versions in a single codebase without a lot of ugly conditionals and boilerplate.

Usage

from pydantic import BaseModel
from pydantic_compat import PydanticCompatMixin
from pydantic_compat import field_validator  # or 'validator'
from pydantic_compat import model_validator  # or 'root_validator'

class MyModel(PydanticCompatMixin, BaseModel):
    x: int
    y: int = 2

    # prefer v2 dict, but v1 class Config is supported
    model_config = {'frozen': True}

    @field_validator('x', mode='after')
    def _check_x(cls, v):
        if v != 42:
            raise ValueError("That's not the answer!")
        return v

    @model_validator('x', mode='after')
    def _check_x(cls, v: MyModel):
        # ...
        return v

You can now use the following attributes and methods regardless of the pydantic version installed (without deprecation warnings):

v1 name v2 name
obj.dict() obj.model_dump()
obj.json() obj.model_dump_json()
obj.copy() obj.model_copy()
Model.construct Model.model_construct
Model.schema Model.model_json_schema
Model.validate Model.model_validate
Model.parse_obj Model.model_validate
Model.parse_raw Model.model_validate_json
Model.update_forward_refs Model.model_rebuild
Model.__fields__ Model.model_fields
Model.__fields_set__ Model.model_fields_set

API rules

  • both V1 and V2 names may be used (regardless of pydantic version), but usage of V2 names are strongly recommended.
  • But the API must match the pydantic version matching the name you are using. For example, if you are using pydantic_compat.field_validator then the signature must match the pydantic (v2) field_validator signature (regardless) of the pydantic version installed. Similarly, if you choose to use pydantic_compat.validator then the signature must match the pydantic (v1) validator signature.

Notable differences

  • BaseModel.__fields__ in v1 is a dict of {'field_name' -> ModelField} whereas in v2 BaseModel.model_fields is a dict of {'field_name' -> FieldInfo}. FieldInfo is a much simpler object that ModelField, so it is difficult to directly support complicated v1 usage of __fields__. pydantic-compat simply provides a name addaptor that lets you access many of the attributes you may have accessed on ModelField in v1 while operating in a v2 world, but ModelField methods will not be made available. You'll need to update your usage accordingly.

  • in V2, pydantic.model_validator(..., mode='after') passes a model instance to the validator function, whereas pydantic.v1.root_validator(..., pre=False) passes a dict of {'field_name' -> validated_value} to the validator function. In pydantic-compat, both decorators follow the semantics of their corresponding pydantic versions, but root_validator gains parameter construct_object: bool=False that matches the model_validator behavior (only when mode=='after'). If you want that behavior though, prefer using model_validator directly.

TODO:

  • Field() objects
  • Serialization decorators

Metadata

Release files for pydantic-compat 0.0.2

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-compat 0.0.2
File Size Uploaded Provenance Actions
pydantic_compat-0.0.2.tar.gz 10.8 kB Without provenance Details for pydantic_compat-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-compat 0.0.2
File Interpreter ABI Platform Actions
pydantic_compat-0.0.2-py3-none-any.whl Python 3 none any Details for pydantic_compat-0.0.2-py3-none-any.whl

Total release size: 22.0 kB

Release files / pydantic_compat-0.0.2.tar.gz

Download URL pydantic_compat-0.0.2.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
958eac9605c9ad483dfd3290d8e213c2c412683ceea380ea11c23a7649762d4c
BLAKE2b-256 checksum
How to use checksums
bed06518be5ba8c225207a0c837a367f4c2f07ed2fff9fea2b6ec46d10461dd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / pydantic_compat-0.0.2-py3-none-any.whl

Download URL pydantic_compat-0.0.2-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b9bac7f444e2b22656cd147e64384b1e3c02afc1bd35d171d68f43f918cda51
BLAKE2b-256 checksum
How to use checksums
fbcb336ad7dc78dc86172b9352c7f174e71062320da5d92d77291e449abe31e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for pydantic-compat 0.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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