Skip to main content

pygraph

Safe, fast, schema-evolvable Python object graph serialization powered by Rust.

Features

  • Drop-in pickle APIdumps/loads/dump/load with the same signatures
  • Zero arbitrary code execution — no __reduce__ or __setstate__ calls during deserialization
  • Schema versioning — migration hooks for evolving your data models
  • Zero-copy tensor support — NumPy and PyTorch integration
  • Rust-native performance — PyO3 bindings for speed
  • Pickle-compatible streamingPickler/Unpickler classes, PickleBuffer (PEP 574)
  • HMAC integrity — optional tamper detection on serialized data

Installation

pip install pygraph

For tensor support:

pip install pygraph[numpy]
pip install pygraph[torch]
pip install pygraph[all]

Quick Start

import pygraph

# Serialize
data = {"key": "value", "numbers": [1, 2, 3]}
encoded = pygraph.dumps(data)

# Deserialize
decoded = pygraph.loads(encoded)
assert decoded == data

Schema Evolution

Define versioned dataclasses and register migrations:

from dataclasses import dataclass
import pygraph

@dataclass
class User:
    name: str
    age: int
    email: str = ""
    __pygraph_version__ = 2

@pygraph.migrate(from_version=1, to_version=2, type_name="User")
def migrate_v1_to_v2(state: dict) -> dict:
    """V1 had 'name' + 'age', V2 adds 'email'."""
    state["email"] = ""
    state["__pygraph_version__"] = 2
    return state

How it works

  • Set __pygraph_version__ as a class attribute on your dataclass
  • Register migration functions with @pygraph.migrate(from_version=N, to_version=M, type_name="ClassName")
  • Migration functions receive a dict of the old state and return a dict with the new state
  • Chains of migrations are resolved automatically (e.g., v1 → v2 → v3)

Pickler / Unpickler

Use pickle-compatible streaming classes:

import pygraph
import io

# Streaming dump
buf = io.BytesIO()
pickler = pygraph.Pickler(buf, protocol=5)
pickler.dump({"data": [1, 2, 3]})

# Streaming load
buf.seek(0)
unpickler = pygraph.Unpickler(buf)
result = unpickler.load()

Security

pygraph never calls __reduce__, __setstate__, or any arbitrary code during deserialization. Only allowlisted types can be loaded.

# Restrict deserialization to specific types
pygraph.loads(data, allowlist={"builtins.dict", "builtins.list"})

Allowlisted types

By default, pygraph supports:

Type Notes
None, bool, int, float Primitives
str, bytes Strings and binary
list, tuple Sequences
dict Mappings
set, frozenset Sets
dataclasses Any @dataclass instance

Any type not in this list raises UnsafeTypeError unless added to the allowlist.

Integrity verification

key = b"my-secret-key"
encoded = pygraph.dumps(data, hmac_key=key)
decoded = pygraph.loads(encoded, hmac_key=key)  # raises HMACError if tampered

Benchmarks

Run the benchmark suite to compare pygraph vs pickle:

pytest benchmarks/ -v --benchmark-only

Development

# Install dev tools
pip install -e ".[dev]"

# Build the Rust extension
maturin develop

# Run tests
cargo nextest run
pytest tests/ -v

License

AGPL-3.0-only

Release files for pysafe-pickle 0.1.0

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

Built distributions (wheels)

Table of built distributions (wheels) for pysafe-pickle 0.1.0
File
pysafe_pickle-0.1.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
pysafe_pickle-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details
pysafe_pickle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
pysafe_pickle-0.1.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
pysafe_pickle-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl CPython 3.10 abi3 Linux glibc 2.34+ x86-64 Details
pysafe_pickle-0.1.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details

Total release size: 1.4 MB

Release files / pysafe_pickle-0.1.0-cp312-cp312-win_amd64.whl

Download URL pysafe_pickle-0.1.0-cp312-cp312-win_amd64.whl
Size 161.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
e4c70af54142f8d056d2a1d0d5399286197c7c632cbc8c85da34a80e9ea1583a
BLAKE2b-256 checksum
How to use checksums
edded6539d589b633eeefcd6b721a1e9b7b68cef9d9ca15af81b04ced6cb0302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release files / pysafe_pickle-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL pysafe_pickle-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 271.3 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
6bddd55fce67a750199b3aac18eef69ff9bb8175252de9a682d06418a11b89c1
BLAKE2b-256 checksum
How to use checksums
18c446816c14b3e72bdbd88158f0646bc01cc67c1ad482e7e89c08f52528854e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release files / pysafe_pickle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL pysafe_pickle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Size 249.7 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
608803b8a645cc7a9cd39e03c05ac2bf5c92b135c9cd3d231ed2eb6a958aeebd
BLAKE2b-256 checksum
How to use checksums
b51fbaa4949821f8e739962664168bb0f37dd205ec104edb2d227e5ac8be5ed0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release files / pysafe_pickle-0.1.0-cp310-abi3-win_amd64.whl

Download URL pysafe_pickle-0.1.0-cp310-abi3-win_amd64.whl
Size 160.4 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e9ebba30a0b716759618b1fd530b2787b3e094023737fc5c7f3055d4633637de
BLAKE2b-256 checksum
How to use checksums
6c023c9a71f68bf341c29641df8ddd1fd3ad9f4088c7fabaeb0374f8de543887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release files / pysafe_pickle-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl

Download URL pysafe_pickle-0.1.0-cp310-abi3-manylinux_2_34_x86_64.whl
Size 272.9 kB
Tags CPython 3.10 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
af024b6a7432ec0c3b1055573cc44ad97d26196d05a89c872d50b66e72f0a323
BLAKE2b-256 checksum
How to use checksums
2ee4cb822102897d0c87701bba880a936a3287e3a38a4db275f84edbd7bd56a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release files / pysafe_pickle-0.1.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL pysafe_pickle-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Size 250.5 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c68b49695a8bbe07727b42c183f05cd93bd212f469fc498da89d1ed987d2466c
BLAKE2b-256 checksum
How to use checksums
3234809ba1e95723a2c6f97fc8fa3b751a42aa5d538b92919acf518e7798597b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 13, 2026.

Transparency log

Release history Release notifications | RSS feed

1.1.0

3 release files

1.0.1

3 release files

1.0.0

3 release files

This release

0.1.0 This release

6 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