msgspec is a fast and friendly implementation of the MessagePack protocol for Python 3.8+. In addition to
serialization/deserialization, it supports message validation using schemas
defined via Python’s type annotations.
from typing import Optional, List import msgspec # Define a schema for a `User` type class User(msgspec.Struct): name: str groups: List[str] =  email: Optional[str] = None # Create a `User` object alice = User("alice", groups=["admin", "engineering"]) # Serialize `alice` to `bytes` using the MessagePack protocol serialized_data = msgspec.encode(alice) # Deserialize and validate the message as a User type user = msgspec.decode(serialized_data, type=User) assert user == alice
msgspecis fast. Benchmarks show it’s among the fastest serialization methods for Python.
msgspecis friendly. Through use of Python’s type annotations, messages can be validated during deserialization in a declaritive way.
msgspecalso works well with other type-checking tooling like mypy, providing excellent editor integration.
msgspecis flexible. Unlike other libraries like
msgspecnatively supports a wider range of Python builtin types. Support for additional types can also be added through extensions.
msgspecsupports “schema evolution”. Messages can be sent between clients with different schemas without error.
msgspec can be installed via
conda. Note that Python >= 3.8
pip install msgspec
conda install msgspec -c conda-forge