Skip to content
/ msgspec Public

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

License

Notifications You must be signed in to change notification settings

jcrist/msgspec

msgspec

msgspecis afastserialization and validation library, with builtin support forJSON,MessagePack, YAML,andTOML.It features:

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

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

  • 🔍Zero-cost schema validationusing familiar Python type annotations. In benchmarksmsgspec decodesandvalidates JSON faster than orjsoncan decode it alone.

  • A speedy Struct typefor representing structured data. If you already usedataclassesor attrs, structsshould feel familiar. However, they're 5-60x faster for common operations.

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


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

Defineyour message schemas using standard Python type annotations.

>>>importmsgspec

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

Encodemessages 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}'

Decodemessages 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(mostrecentcalllast):
File"<stdin>",line1,in<module>
msgspec.ValidationError:Expected`str`,got`int`-at`$.groups[0]`

msgspecis 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 withmsgspeccan be ~10-80x faster than alternative libraries.

Seethe documentationfor more information.

LICENSE

New BSD. See the License File.