serpyco-rs: High-Performance Dataclass Serialization and Validation

1.20.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This example demonstrates how to define a dataclass, create a serializer for it, and then use the serializer to dump a dataclass instance to a dictionary, load a dictionary into a dataclass instance, and generate a JSON Schema for the defined structure.

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}")

view raw JSON →