Skip to main content

Unified Python serializer with msgspec MessagePack backend

Project description

pypackd

Unified Python serializer with msgspec MessagePack backend. One API for all your types — Pydantic, msgspec Structs, dataclasses, and every stdlib type.

Installation

pip install pypackd

With Pydantic support:

pip install pypackd[pydantic]

Usage

from serializer import Serializer

# Serialize any supported type
data = Serializer.serialize(my_object)

# Deserialize with target type
obj = Serializer.deserialize(data, MyType)

Primitives & Collections

Serializer.deserialize(Serializer.serialize(42), int)           # 42
Serializer.deserialize(Serializer.serialize("hello"), str)      # "hello"
Serializer.deserialize(Serializer.serialize([1, 2, 3]), list)   # [1, 2, 3]
Serializer.deserialize(Serializer.serialize({10, 20}), set)     # {10, 20}

Dataclasses

from dataclasses import dataclass

@dataclass
class Point:
    x: float
    y: float

p = Point(3.14, 2.72)
data = Serializer.serialize(p)
Serializer.deserialize(data, Point)  # Point(x=3.14, y=2.72)

msgspec Structs

import msgspec

class Config(msgspec.Struct):
    host: str
    port: int
    debug: bool = False

cfg = Config(host="localhost", port=8080)
data = Serializer.serialize(cfg)
Serializer.deserialize(data, Config)  # Config(host='localhost', port=8080, debug=False)

Pydantic Models

from pydantic import BaseModel
from datetime import datetime
from uuid import UUID

class User(BaseModel):
    id: UUID
    name: str
    created_at: datetime

user = User(id=UUID("abcdef01-2345-6789-abcd-ef0123456789"), name="Georg", created_at=datetime.now())
data = Serializer.serialize(user)
Serializer.deserialize(data, User)  # User(id=UUID('abcdef01-...'), name='Georg', ...)

Nested Types

Pydantic models with dataclass fields, nested Pydantic models, or any combination — it just works.

@dataclass
class Address:
    street: str
    city: str

class Order(BaseModel):
    order_id: int
    customer: User
    shipping: Address
    items: list[str]

order = Order(order_id=1, customer=user, shipping=Address("Hauptstr. 1", "Berlin"), items=["Laptop"])
data = Serializer.serialize(order)
Serializer.deserialize(data, Order)  # Full roundtrip with all nested types

Supported Types

Category Types
Primitives str, int, float, bool, None, bytes
Collections list, dict, tuple, set, frozenset
Datetime datetime, date, time, timedelta
Stdlib UUID, Decimal, Enum
Structured dataclass, NamedTuple, TypedDict, msgspec.Struct
Pydantic BaseModel, RootModel (incl. computed fields, aliases, excluded fields, custom serializers, model/field validators)
Typing Optional, Union, Literal, generics

How It Works

pypackd is a thin adapter layer over msgspec, which handles all non-Pydantic types natively with zero overhead. For Pydantic models, pypackd auto-detects the model's features and chooses the fastest serialization path:

  • Simple modelsobj.__dict__ (2-5x faster than model_dump())
  • RootModel, models with computed fields, aliases, excluded fields, or custom serializersmodel_dump(mode='python') for correctness

Type classifications are cached, Encoder/Decoder instances are reused, and all cache writes are thread-safe. 292 tests, 99% code coverage.

Error Handling

All errors are wrapped in a unified exception hierarchy:

from serializer import SerializerError, SerializeError, DeserializeError

try:
    Serializer.deserialize(data, MyModel)
except DeserializeError as e:
    print(e)            # Descriptive message
    print(e.__cause__)  # Original pydantic/msgspec exception

Requirements

  • Python 3.10+
  • msgspec >= 0.18
  • pydantic >= 2.0 (optional)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypackd-0.1.1.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypackd-0.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pypackd-0.1.1.tar.gz.

File metadata

  • Download URL: pypackd-0.1.1.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypackd-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6a67df395f755c103214b267e5eb4ad43e85535b04a622d6af5e08bb92a514b1
MD5 d7d5d6789380ea731ae48dc90b7900bc
BLAKE2b-256 d4afff48c2c19dc055b61774ade55aa57d56ddb1f25bf46de376398e29d513c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypackd-0.1.1.tar.gz:

Publisher: tests.yml on Schorschofwill/pypackd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypackd-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pypackd-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pypackd-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b31f8eea7b5ac28dad069279574bd931f477d2744c9bedc3ef4b06abb843855
MD5 dee9521d6565a9d09b42c85514825d9c
BLAKE2b-256 f9961016e0d4a6abd8d373b13020501b3e2dde5b09014efb9e7ca56c81932f90

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypackd-0.1.1-py3-none-any.whl:

Publisher: tests.yml on Schorschofwill/pypackd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page