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-0.18.5.tar.gz (210.4 kB view hashes)

Uploaded Source

Built Distributions

msgspec-0.18.5-cp312-cp312-win_amd64.whl (180.4 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

msgspec-0.18.5-cp312-cp312-musllinux_1_1_x86_64.whl (209.2 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

msgspec-0.18.5-cp312-cp312-musllinux_1_1_aarch64.whl (206.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

msgspec-0.18.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (205.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

msgspec-0.18.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (203.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

msgspec-0.18.5-cp312-cp312-macosx_11_0_arm64.whl (183.8 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

msgspec-0.18.5-cp312-cp312-macosx_10_9_x86_64.whl (195.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

msgspec-0.18.5-cp311-cp311-win_amd64.whl (179.2 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

msgspec-0.18.5-cp311-cp311-musllinux_1_1_x86_64.whl (207.4 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

msgspec-0.18.5-cp311-cp311-musllinux_1_1_aarch64.whl (205.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

msgspec-0.18.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (202.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

msgspec-0.18.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (201.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

msgspec-0.18.5-cp311-cp311-macosx_11_0_arm64.whl (182.9 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

msgspec-0.18.5-cp311-cp311-macosx_10_9_x86_64.whl (193.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

msgspec-0.18.5-cp310-cp310-win_amd64.whl (179.2 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

msgspec-0.18.5-cp310-cp310-musllinux_1_1_x86_64.whl (208.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

msgspec-0.18.5-cp310-cp310-musllinux_1_1_aarch64.whl (206.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

msgspec-0.18.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (203.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

msgspec-0.18.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (202.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

msgspec-0.18.5-cp310-cp310-macosx_11_0_arm64.whl (185.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

msgspec-0.18.5-cp310-cp310-macosx_10_9_x86_64.whl (196.1 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

msgspec-0.18.5-cp39-cp39-win_amd64.whl (179.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

msgspec-0.18.5-cp39-cp39-musllinux_1_1_x86_64.whl (208.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

msgspec-0.18.5-cp39-cp39-musllinux_1_1_aarch64.whl (206.5 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

msgspec-0.18.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (203.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

msgspec-0.18.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (202.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

msgspec-0.18.5-cp39-cp39-macosx_11_0_arm64.whl (185.2 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

msgspec-0.18.5-cp39-cp39-macosx_10_9_x86_64.whl (196.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

msgspec-0.18.5-cp38-cp38-win_amd64.whl (181.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

msgspec-0.18.5-cp38-cp38-musllinux_1_1_x86_64.whl (209.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

msgspec-0.18.5-cp38-cp38-musllinux_1_1_aarch64.whl (207.3 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

msgspec-0.18.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (205.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

msgspec-0.18.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (203.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

msgspec-0.18.5-cp38-cp38-macosx_11_0_arm64.whl (184.6 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

msgspec-0.18.5-cp38-cp38-macosx_10_9_x86_64.whl (195.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

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