Skip to main content

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML.

Project description

msgspec

msgspec is a fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML. It features:

  • 🚀 High performance encoders/decoders for common protocols. The JSON and MessagePack implementations regularly benchmark as the fastest options for Python.

  • 🎉 Support for a wide variety of Python types. Additional types may be supported through extensions.

  • 🔍 Zero-cost schema validation using familiar Python type annotations. In benchmarks msgspec decodes and validates JSON faster than orjson can decode it alone.

  • A speedy Struct type for representing structured data. If you already use dataclasses or attrs, structs should feel familiar. However, they're 5-60x faster for common operations.

All of this is included in a lightweight library with no required dependencies.


msgspec may be used for serialization alone, as a faster JSON or MessagePack library. For the greatest benefit though, we recommend using msgspec to handle the full serialization & validation workflow:

Define your message schemas using standard Python type annotations.

>>> import msgspec

>>> class User(msgspec.Struct):
...     """A new type describing a User"""
...     name: str
...     groups: set[str] = set()
...     email: str | None = None

Encode messages as JSON, or one of the many other supported protocols.

>>> alice = User("alice", groups={"admin", "engineering"})

>>> alice
User(name='alice', groups={"admin", "engineering"}, email=None)

>>> msg = msgspec.json.encode(alice)

>>> msg
b'{"name":"alice","groups":["admin","engineering"],"email":null}'

Decode messages back into Python objects, with optional schema validation.

>>> msgspec.json.decode(msg, type=User)
User(name='alice', groups={"admin", "engineering"}, email=None)

>>> msgspec.json.decode(b'{"name":"bob","groups":[123]}', type=User)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `str`, got `int` - at `$.groups[0]`

msgspec is designed to be as performant as possible, while retaining some of the nicities of validation libraries like pydantic. For supported types, encoding/decoding a message with msgspec can be ~10-80x faster than alternative libraries.

See the documentation for more information.

LICENSE

New BSD. See the License File.

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

msgspec_python313_pre-0.19.2.tar.gz (316.6 kB view details)

Uploaded Source

Built Distribution

msgspec_python313_pre-0.19.2-cp313-cp313-macosx_15_0_x86_64.whl (223.1 kB view details)

Uploaded CPython 3.13 macOS 15.0+ x86-64

File details

Details for the file msgspec_python313_pre-0.19.2.tar.gz.

File metadata

  • Download URL: msgspec_python313_pre-0.19.2.tar.gz
  • Upload date:
  • Size: 316.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.20.0.post1 CPython/3.13.0 Darwin/24.1.0

File hashes

Hashes for msgspec_python313_pre-0.19.2.tar.gz
Algorithm Hash digest
SHA256 b7d4e8f8872925c4dd13e5addbaa18f0f26876dfd9754d04d4b2ed064d6d1e06
MD5 b7c15ebda383fa8cb612f09270f368fa
BLAKE2b-256 6a63afbe2a91239d4d4c9b278221b43c24d5ab5d5b8870b8a7d16ee7508a5a30

See more details on using hashes here.

File details

Details for the file msgspec_python313_pre-0.19.2-cp313-cp313-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for msgspec_python313_pre-0.19.2-cp313-cp313-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 818d80b5d9e5934595b0c6e9fc90a25fa5b1b55eb259342085eb77ad8d37d21f
MD5 980370dacfb9706c0d87afb0ebec6823
BLAKE2b-256 7577f076f2d1ced1e8913360174920a68b067c52610a01242be39305304b09f0

See more details on using hashes here.

Supported by

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