serpyco-rs: High-Performance Dataclass Serialization and Validation
Serpyco-rs is a high-performance Python library for serializing and deserializing dataclasses, inspired by `serpyco`. It leverages Rust for speed and provides features like data validation, JSON Schema generation (Draft 2020-12), and custom encoder/decoder support. It supports Python 3.9+ and maintains a frequent release cadence, with version 1.20.0 being the latest as of March 2026.
Warnings
- breaking As of v1.20.0, `Min` and `Max` validators are now inclusive by default. If your existing code or schemas relied on exclusive behavior without explicitly setting an `inclusive` flag, this change will alter validation logic.
- gotcha Prior to v1.17.1, boolean values (`true`/`false`) were incorrectly accepted as integers during validation. If you were implicitly relying on this behavior, validation might now fail for such inputs.
- gotcha For JSON Schema generation, simple union types now correctly generate `anyOf` constructs (since v1.17.0) and integer fields explicitly include `"format": "int64"` (since v1.15.0). These changes improve JSON Schema compliance but might cause discrepancies for consumers expecting older schema formats.
Install
-
pip install serpyco-rs
Imports
- Serializer
from serpyco_rs import Serializer
- dataclass
from dataclasses import dataclass
Quickstart
import dataclasses
from serpyco_rs import Serializer
@dataclasses.dataclass
class User:
id: int
name: str
email: str
is_active: bool = True
# Create a serializer for the User dataclass
user_serializer = Serializer(User)
# Create a User instance
user_instance = User(id=1, name="Alice", email="alice@example.com")
# Serialize the dataclass instance to a dictionary
dumped_data = user_serializer.dump(user_instance)
print(f"Dumped data: {dumped_data}")
# Expected: {'id': 1, 'name': 'Alice', 'email': 'alice@example.com', 'is_active': True}
# Load data from a dictionary into a dataclass instance
loaded_data = {'id': 2, 'name': 'Bob', 'email': 'bob@example.com', 'is_active': False}
user_from_dict = user_serializer.load(loaded_data)
print(f"Loaded instance: {user_from_dict}")
# Expected: User(id=2, name='Bob', email='bob@example.com', is_active=False)
# Generate JSON Schema for the dataclass
json_schema = user_serializer.json_schema()
print(f"JSON Schema: {json_schema}")