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


  • msgspec is fast. Benchmarks show it’s among the fastest serialization methods for Python.

  • msgspec is friendly. Through use of Python’s type annotations, messages can be validated during deserialization in a declaritive way. msgspec also works well with other type-checking tooling like mypy, providing excellent editor integration.

  • msgspec is flexible. Unlike other libraries like msgpack or json, msgspec natively supports a wider range of Python builtin types. Support for additional types can also be added through extensions.

  • msgspec supports “schema evolution”. Messages can be sent between clients with different schemas without error.


msgspec can be installed via pip or conda. Note that Python >= 3.8 is required.


pip install msgspec


conda install msgspec -c conda-forge