Registry / serialization / pydantic-compat

pydantic-compat

JSON →
library0.1.2pypypi✓ verified 85d ago

pydantic-compat is a compatibility layer designed to help developers write code that works seamlessly with both Pydantic v1 and v2. It provides common interfaces, aliases, and wrappers for key Pydantic components, allowing libraries and applications to support different Pydantic major versions without extensive conditional logic. The current version is 0.1.2, and it follows an as-needed release cadence to address compatibility needs.

pip install pydantic-compat
INSTALL
IMPORT
SIG · PYDANTIC-COMPAT
P
pydantic-compat
serializationpythonv0.1.2
Install
3.2s avg
Import
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.2 · 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.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 27.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.2s · import 0.000s · 28MB
26MB installed
● package 26MB
Code
Verified usage

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

BaseModel
from pydantic_compat import BaseModel
from pydantic_compat import CompatModel
Field
from pydantic_compat import Field
validator
from pydantic_compat import validator

This quickstart demonstrates how to define a model using `CompatModel` and use `model_validator` for cross-version compatible validation. It also shows how to leverage `ConfigDict` for defining model configurations that adapt to Pydantic v1's `class Config` or v2's `model_config`.

from pydantic_compat import CompatModel, model_validator, FieldValidationInfo from typing import ClassVar import pydantic # To check version directly if needed class MyConfig(CompatModel): my_field: str version_info: ClassVar[str] = "" @model_validator(mode='after') def check_version(self, info: FieldValidationInfo): # Access original pydantic version for conditional logic if pydantic.VERSION.startswith('1.'): self.version_info = "Pydantic v1 compatible" else: self.version_info = "Pydantic v2 compatible" return self # This will adapt based on the installed Pydantic version config_instance = MyConfig(my_field="hello world") print(f"MyField: {config_instance.my_field}") print(f"Compatibility Info: {config_instance.version_info}") # Example with ConfigDict (for Pydantic v2 style config) from pydantic_compat import ConfigDict class MyModelWithConfig(CompatModel): model_config = ConfigDict(extra='ignore', frozen=True) value: int model_instance = MyModelWithConfig(value=123, unknown_field="ignored") print(f"Model Configured Value: {model_instance.value}")
Debug
Known issues
gotchapydantic-compat provides *syntactic* compatibility for common patterns, not full feature translation. It does not automatically enable Pydantic v2-only features when Pydantic v1 is installed, or vice-versa. Its primary goal is to allow a single codebase to define models and validators using a common API.
fix
Understand that you might still need conditional logic based on `pydantic.VERSION` for truly version-specific behavior or access to features not present in both major versions.
affects: All
gotchaMixing `pydantic.BaseModel` and `pydantic_compat.CompatModel` in the same codebase (especially for inheritance) can lead to unexpected behavior or `AttributeError` if the underlying Pydantic version doesn't support the syntax used with `pydantic.BaseModel`.
fix
Always use `pydantic_compat.CompatModel` as the base for any model intended to be compatible across Pydantic v1 and v2. Ensure all relevant validators and configurations also use `pydantic_compat` imports.
affects: All
gotchaPydantic v1 uses `class Config:` nested classes, while v2 uses `model_config = {...}` or `ConfigDict`. While `pydantic-compat` provides `ConfigDict`, developers need to be mindful of which configuration options are truly available and behave identically across versions.
fix
Consult Pydantic's official migration guide for differences in configuration options. When in doubt, use options known to be common, or apply conditional logic based on `pydantic.VERSION` for specific configurations.
affects: All
Upgrade
Version history
0.1.2latest on PyPI · released Oct 24, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
pydantic-compat — pip install pydantic-compat · libregistry