Library / SDK
msgspec/msgspec avatar
msgspec/msgspec

msgspec validates while it decodes, so a bad payload fails with a path like $.groups[0]

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

4,230 stars235 forksPythonBSD-3-Clause

At a glance

What is it?
msgspec is a Python library for encoding and decoding JSON, MessagePack, YAML and TOML with schema validation driven by type annotations. Its speed claims come from its own benchmarks, and it has no required dependencies.
Who is it for?
Adopt msgspec if you decode JSON or MessagePack in Python and want validation to happen in the same call, with Struct types in place of dataclasses. Hold off if you need a 32-bit Windows build, a Python older than 3.10, or an interpreter other than CPython.
Can I use it commercially?
Yes. BSD-3-Clause is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • msgspec/msgspec4,230 ★ · +75
Star history
Repository2026-10-082026-11-24
msgspec/msgspec4.2k4.2k

75 Stars in the last 47 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Decoding and validation happen in a single msgspec.json.decode call

The distinguishing idea is that validation is not a separate step. You pass a type to the decoder and the library checks the payload against it while parsing. The README's example decodes a valid message back into a `User` and then shows what a bad one does:

python
>>> 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]`

The error names the expected type, the type it found and the location inside the document, `$.groups[0]`. That path tells you which element of which list was wrong without a second pass over the data.

The README calls this zero-cost schema validation using familiar Python type annotations, and says that in the project's benchmarks msgspec decodes and validates JSON faster than orjson can decode it alone. That ratio is the project's own measurement. The benchmark page at msgspec.dev/benchmarks holds the numbers, so check your own data shapes against it.

Struct is the type you define, encode and decode with

A schema is a class that inherits from `msgspec.Struct`, with fields declared as annotations. The README defines one:

python
>>> import msgspec

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

Fields without defaults are required, and `groups` and `email` have defaults. Creating an instance and encoding it to JSON takes two calls:

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

The encoded result is bytes, not a string. Note that the Python set `groups` comes out as a JSON array.

For people who already use dataclasses or attrs, the README says structs should feel familiar, and claims they are 5-60x faster for common operations. The speedup is attributed to the project's structs benchmark. The README does not list which operations fall at each end of that range.

Four formats, with the heavier two behind optional extras

msgspec has builtin support for JSON, MessagePack, YAML and TOML. The README lists no install command, only badges linking to PyPI and conda-forge, so those are the two places to get it.

The core library has no required dependencies. In `pyproject.toml` the optional groups are `toml`, which pulls `tomli` for Python before 3.11 plus `tomli_w`, and `yaml`, which pulls `pyyaml`. JSON and MessagePack need nothing extra. So a service that only speaks JSON ships msgspec alone, and the YAML and TOML codecs arrive only if you ask for those groups.

The README says the JSON and MessagePack implementations regularly benchmark as the fastest options for Python. YAML and TOML get no speed claim.

Types beyond the builtin ones can be supported through extensions, documented at msgspec.dev/extending. The README does not list the supported types in the text, so check the documentation for yours before assuming a custom class round-trips.

How the README positions msgspec against pydantic, orjson and attrs

The README draws three comparisons, and each cites the project's benchmark page. Against orjson, it claims msgspec decodes and validates JSON faster than orjson can decode alone. Against dataclasses and attrs, it claims structs are 5-60x faster for common operations. Against alternative libraries in general, it claims encoding and decoding can be 10-80x faster for supported types.

Pydantic is the one it names as a model for convenience: msgspec is designed to be as performant as possible while retaining some of the niceties of validation libraries like pydantic. That sentence says what msgspec is trying to keep, and it does not claim feature parity. Pydantic's validators, settings management and similar features are not mentioned, so do not assume equivalents exist.

The approach differs in one concrete way. msgspec asks you to define schemas as `Struct` classes and decode into them. Dataclasses and attrs users will find the model familiar but will need to move their types over.

The README says msgspec may be used for serialization alone, as a faster JSON or MessagePack library, and recommends the full serialization and validation workflow for the greatest benefit.

Python 3.10 or newer on CPython, with no 32-bit Windows

The `pyproject.toml` sets `requires-python = ">=3.10"`. Its classifiers list CPython 3.10 through 3.15 and mark the implementation as CPython only. Python 3.9 and earlier are out, and PyPy is not listed.

The development status classifier reads 4 - Beta, and the version in the recent releases is still below 1.0: 0.21.0 on 8 April 2026, 0.21.1 on 12 April 2026 and 0.22.0 on 29 September 2026. Pin the version and read the release notes before upgrading.

`setup.py` blocks one platform on purpose. On a 32-bit Python build under Windows it prints an error saying msgspec currently doesn't support 32-bit Python Windows builds, and exits. The message points to issue 845 for anyone who needs it. The check uses `sys.maxsize` and not `platform.architecture`, because users can run 32-bit Python on a 64-bit machine.

The classifiers also include an Emscripten environment entry. The README does not discuss WebAssembly, so do not infer a supported workflow from that one line.

Building from source exposes sanitizer, coverage and debug switches

msgspec has a C extension, so installing from source compiles code. `setup.py` reads three environment variables. `MSGSPEC_SANITIZE` adds `-fsanitize=address` and `-fsanitize=undefined` to the compile flags, along with `-lasan` and `-lubsan` at link time. `MSGSPEC_COVERAGE` adds `--coverage` and `-lgcov`. `MSGSPEC_DEBUG` defaults to on when either of the others is set, and switches to `-O0`, `-g` and `-UNDEBUG`.

Without debug, non-Windows builds get `-g0`, and Apple Silicon macOS builds add `-flto=thin` for both compile and link. On Windows the build adds `/std:c11` among its flags. The setup.py comment links to a free-threading FAQ about C atomic support on MSVC.

These switches matter to contributors and packagers, not to people who install a wheel. The repository root also has `.cibuildwheel.toml`, along with `tests/`, `benchmarks/` and `examples/`. The examples folder holds `asyncio-kv`, `conda-repodata`, `geojson` and `pyproject-toml`, which are the project's own sample programs.

A community-maintained project with a Beta label and recent pushes

The `pyproject.toml` lists Jim Crist-Harif and The msgspec community as authors. Maintainers include The msgspec community, Ofek Lev, Nikita Sobolev, Janek Nouvertné and Tseluiko Aleksandr. The license is BSD-3-Clause, which the README calls New BSD, and the full text is in the `LICENSE` file.

The repository is not archived. The last push was on 28 October 2026, about four weeks after 0.22.0 shipped on 29 September, so work continues on the main branch past the last tag.

The README itself is short and sends you to msgspec.dev for everything else. It does not document the Struct options, the extension API, the YAML and TOML behaviour or migration from pydantic. If you evaluate msgspec for production, read the documentation site, then run the benchmark yourself against realistic payloads, then test your hardest schema, such as nested optional fields, before committing.

Editorial conclusion

Adopt msgspec if you decode JSON or MessagePack in Python and want validation to happen in the same call, with Struct types in place of dataclasses. Hold off if you need a 32-bit Windows build, a Python older than 3.10, or an interpreter other than CPython. Before switching a service over, rerun the project's benchmark page against your own payloads, since the speed ratios in the README are the project's own measurements.

Frequently asked questions

msgspec vs pydantic v2: which is faster?

The README claims that for supported types, encoding and decoding with msgspec can be 10-80x faster than alternative libraries, and points to its benchmark page. It says msgspec keeps some of the niceties of validation libraries like pydantic.

How does msgspec compare to attrs?

The README says that if you already use dataclasses or attrs, msgspec Struct types should feel familiar, and that they are 5-60x faster for common operations according to the project's benchmarks.

How does msgspec compare to msgpack?

msgspec has builtin MessagePack support alongside JSON, YAML and TOML. The README says its JSON and MessagePack implementations regularly benchmark as the fastest options for Python.

Official sources

  1. License: BSD-3-Clause
  2. msgspec/msgspec on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/msgspec-msgspec.svg)](https://hysenlabs.com/projects/msgspec-msgspec)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/msgspec-msgspec.svg)](https://hysenlabs.com/projects/msgspec-msgspec)

Was this useful?