# msgspec/msgspec

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/msgspec-msgspec).**

3,821 stars · 155 forks · Python · BSD-3-Clause

## Links

- GitHub: https://github.com/msgspec/msgspec
- Homepage: https://msgspec.dev
- awesome-repositories: https://awesome-repositories.com/repository/msgspec-msgspec.md

## Topics

`deserialization` `json` `json-schema` `jsonschema` `messagepack` `msgpack` `openapi3` `python` `schema` `serde` `serialization` `toml` `validation` `yaml`

## Description

msgspec is a high-performance data modeling, serialization, and schema validation toolkit for Python. It serves as a type-safe serialization framework that integrates schema enforcement and data parsing into a single pass, functioning as both a data serialization library and a schema validation system based on standard Python type annotations.

The project distinguishes itself through high-performance structural primitives, including compilation-based routine generation and zero-copy buffer parsing. It optimizes memory usage via garbage collection-aware layouts and reduces processing overhead by performing structural verification and deserialization in one operation.

The toolkit provides multi-format support for JSON, MessagePack, YAML, and TOML, alongside capabilities for polymorphic type deserialization and schema evolution management. It includes data validation primitives for type coercion and constraint enforcement, as well as utilities for transforming complex structured objects into builtin types.

The framework also enables the creation of optimized data containers that provide faster instantiation and lower memory overhead than standard classes.

## Tags

### Data & Databases

- [High-Performance Binary Serialization](https://awesome-repositories.com/f/data-databases/high-performance-binary-serialization.md) — Offers high-performance serialization for JSON, MessagePack, YAML, and TOML with minimal CPU and memory overhead.
- [Multi-Format Serializers](https://awesome-repositories.com/f/data-databases/multi-format-serializers.md) — Provides high-performance serialization and deserialization across multiple formats including JSON, MessagePack, YAML, and TOML. ([source](https://msgspec.dev))
- [Complex Data Types](https://awesome-repositories.com/f/data-databases/complex-data-types.md) — Supports encoding and decoding of non-scalar types like UUIDs, decimals, and datetimes using type annotations. ([source](https://msgspec.dev/supported-types.html))
- [TOML Parsers](https://awesome-repositories.com/f/data-databases/data-engineering-infrastructure/data-extraction-ingestion/data-parsing/toml-parsers.md) — Converts TOML strings or bytes into typed Python structures for application use. ([source](https://msgspec.dev/api.html))
- [TOML Serializers](https://awesome-repositories.com/f/data-databases/data-serialization-formats/toml-serializers.md) — Converts Python objects into TOML format with support for transforming field names to kebab-case. ([source](https://msgspec.dev/api.html))
- [Optimized Data Containers](https://awesome-repositories.com/f/data-databases/data-structure-definitions/optimized-data-containers.md) — Creates structured objects optimized for fast instantiation, equality comparison, and ordering compared to standard classes. ([source](https://msgspec.dev/benchmarks.html))
- [Type-Safe Structured Data Frameworks](https://awesome-repositories.com/f/data-databases/data-type-managers/structured-types/type-safe-structured-data-frameworks.md) — Integrates schema enforcement and data parsing into a single pass to achieve near-native execution speeds.
- [MessagePack Serializers](https://awesome-repositories.com/f/data-databases/high-performance-binary-serialization/messagepack-serializers.md) — Converts MessagePack binary bytes into typed structures using custom hooks and extensions. ([source](https://msgspec.dev/api.html))
- [High-Performance Containers](https://awesome-repositories.com/f/data-databases/high-performance-containers.md) — Provides optimized data containers with faster instantiation and lower memory overhead than standard dataclasses.
- [JSON Deserializers](https://awesome-repositories.com/f/data-databases/json-deserializers.md) — Transforms JSON bytes into typed Python structures using strict type coercion and custom hooks. ([source](https://msgspec.dev/api.html))
- [JSON Serializers](https://awesome-repositories.com/f/data-databases/json-serializers.md) — Converts Python objects into formatted JSON bytes with support for deterministic key ordering. ([source](https://msgspec.dev/api.html))
- [Schema Validation Libraries](https://awesome-repositories.com/f/data-databases/schema-validation-libraries.md) — A validation system using standard Python type annotations to enforce structural integrity during deserialization.
- [YAML Data Serialization](https://awesome-repositories.com/f/data-databases/yaml-data-serialization.md) — Converts YAML bytes into typed structures for internal application processing. ([source](https://msgspec.dev/api.html))
- [Zero-Copy Parsing](https://awesome-repositories.com/f/data-databases/zero-copy-data-access-libraries/zero-copy-deserialization/zero-copy-parsing.md) — Processes serialized byte streams by referencing memory directly to avoid creating intermediate Python objects.
- [Serialization Hooks](https://awesome-repositories.com/f/data-databases/data-type-managers/dynamic-type-managers/custom-type-serializers/custom-serialization-interfaces/custom-serializer-implementations/serialization-hooks.md) — Converts non-standard types into serializable formats using custom runtime hook functions. ([source](https://msgspec.dev/porting/orjson.html))
- [Data Type Serialization](https://awesome-repositories.com/f/data-databases/data-type-managers/dynamic-type-managers/custom-type-serializers/data-type-serialization.md) — Converts complex objects into simple builtin types to ensure compatibility with external serialization libraries. ([source](https://msgspec.dev/converters.html))
- [Schema Evolution](https://awesome-repositories.com/f/data-databases/data-type-schemas/schema-evolution.md) — Enables exchanging messages between different schema versions without triggering errors as data formats change. ([source](https://msgspec.dev/why.html))
- [Extension Type Management](https://awesome-repositories.com/f/data-databases/high-performance-binary-serialization/messagepack-serializers/extension-type-management.md) — Serializes custom objects as binary extension types using integer codes and byte buffers to maintain type information in MessagePack. ([source](https://msgspec.dev/extending.html))
- [Polymorphic Deserialization](https://awesome-repositories.com/f/data-databases/polymorphic-data-modeling/polymorphic-deserialization.md) — Maps input to the correct object representation using tags to distinguish between multiple types in a union. ([source](https://msgspec.dev/examples/geojson.html))
- [Selective JSON Parsing](https://awesome-repositories.com/f/data-databases/selective-json-parsing.md) — Parses only specific fields defined in a schema to reduce memory allocations and increase decoding speed. ([source](https://msgspec.dev/examples/conda-repodata.html))
- [Newline-Delimited JSON Streams](https://awesome-repositories.com/f/data-databases/streaming-parsers/json/newline-delimited-json-streams.md) — Encodes and decodes sequences of objects as newline-delimited JSON for efficient stream processing. ([source](https://msgspec.dev/porting/orjson.html))

### Operating Systems & Systems Programming

- [Garbage Collection Tuning](https://awesome-repositories.com/f/operating-systems-systems-programming/kernel-core-internals/process-and-memory-management/memory-management/automated-reclamation-systems/garbage-collection-tuning.md) — Lowers garbage collection overhead by disabling tracking for structure types that do not contain reference cycles. ([source](https://msgspec.dev/perf-tips.html))
- [GC-Aware Memory Layouts](https://awesome-repositories.com/f/operating-systems-systems-programming/kernel-core-internals/process-and-memory-management/memory-management/automated-reclamation-systems/garbage-collection/gc-aware-memory-layouts.md) — Lowers memory usage and shortens pause times by optimizing the internal memory layout of structured objects. ([source](https://msgspec.dev/benchmarks.html))

### Programming Languages & Runtimes

- [Single-Pass Parsing](https://awesome-repositories.com/f/programming-languages-runtimes/bytecode-compilation/single-pass-compilation/single-pass-parsing.md) — Integrates data deserialization and schema validation into a single pass to eliminate redundant processing overhead.
- [JIT Routine Generation](https://awesome-repositories.com/f/programming-languages-runtimes/jit-routine-generation.md) — Transforms type annotations into specialized machine routines to perform parsing and validation in a single high-performance pass.
- [Python Serialization Libraries](https://awesome-repositories.com/f/programming-languages-runtimes/python-serialization-libraries.md) — A high-performance toolkit for encoding and decoding Python objects using JSON, MessagePack, YAML, and TOML.
- [High-Speed Type Conversion](https://awesome-repositories.com/f/programming-languages-runtimes/high-speed-type-conversion.md) — Provides high-speed transformation of complex structured objects into plain Python builtin types. ([source](https://msgspec.dev/usage.html))
- [Object Type Transformations](https://awesome-repositories.com/f/programming-languages-runtimes/object-type-transformations.md) — Transforms input objects into specified types by extracting attributes from models or coercing keys. ([source](https://msgspec.dev/api.html))
- [Frozen Instance Builders](https://awesome-repositories.com/f/programming-languages-runtimes/slotted-class-definitions/frozen-instance-builders.md) — Prevents attribute modification after initialization and enables hashing by marking data structures as frozen. ([source](https://msgspec.dev/structs.html))
- [Tagged Unions](https://awesome-repositories.com/f/programming-languages-runtimes/tagged-unions.md) — Implements tagged unions by adding type identifiers to serialized data to distinguish between structures in a union. ([source](https://msgspec.dev/structs.html))

### Software Engineering & Architecture

- [YAML Serializers](https://awesome-repositories.com/f/software-engineering-architecture/application-lifecycle-management/configuration-management/configuration-formats-and-schemas/yaml-configuration-files/yaml-serializers.md) — Serializes Python objects into YAML bytes for storage or transmission. ([source](https://msgspec.dev/api.html))
- [Class-Based Data Modeling](https://awesome-repositories.com/f/software-engineering-architecture/class-based-data-modeling.md) — Provides a system for modeling data using classes to encapsulate state and behavior with high-performance instantiation. ([source](https://msgspec.dev/why.html))
- [Data Schema Validation](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-validation.md) — Enforces data integrity by verifying that incoming serialized data matches Python type annotations and constraints.
- [Type-Annotation Validation](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-validation/data-type-validation/type-annotation-validation.md) — Uses standard Python type hints as the formal blueprint for generating high-performance serialization and validation logic.
- [Binary Data Processing](https://awesome-repositories.com/f/software-engineering-architecture/type-safe-data-handling/binary-data-processing.md) — Provides efficient binary serialization using MessagePack and zero-copy buffer parsing for high throughput.
- [Typed Payload Decoders](https://awesome-repositories.com/f/software-engineering-architecture/workflow-automation-patterns/determinism-validators/configuration-validators/external-api-validators/external-data-validation/typed-payload-decoders.md) — Decodes untrusted input into typed, validated structures using Python type annotations. ([source](https://msgspec.dev/porting/orjson.html))
- [Buffer-Reusing Serializers](https://awesome-repositories.com/f/software-engineering-architecture/zero-copy-buffers/zero-allocation-buffer-encoders/buffer-reusing-serializers.md) — Reduces CPU and memory overhead by reusing encoder instances and writing into pre-allocated buffers. ([source](https://msgspec.dev/perf-tips.html))
- [Custom Type Decoders](https://awesome-repositories.com/f/software-engineering-architecture/custom-format-decoders/custom-type-decoders.md) — Transforms non-native types into supported values during encoding and decoding using custom callback functions. ([source](https://msgspec.dev/extending.html))
- [Union Type Validation](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-validation/data-type-validation/union-type-validation.md) — Decodes values into one of several possible types by identifying the single compatible type in a union. ([source](https://msgspec.dev/supported-types.html))
- [Default Value Omission](https://awesome-repositories.com/f/software-engineering-architecture/default-configuration-values/schema-default-values/default-value-omission.md) — Decreases message size by omitting fields that match their default values during encoding. ([source](https://msgspec.dev/structs.html))
- [Forward and Backward Compatibility](https://awesome-repositories.com/f/software-engineering-architecture/default-configuration-values/schema-default-values/forward-and-backward-compatibility.md) — Decodes messages across different versions by using default values for missing fields and skipping unknown fields. ([source](https://msgspec.dev/schema-evolution.html))
- [Deferred Decoding](https://awesome-repositories.com/f/software-engineering-architecture/deferred-decoding.md) — Provides a view into encoded messages to decode specific fields only after others are processed. ([source](https://msgspec.dev/api.html))
- [Memory Layout Optimizations](https://awesome-repositories.com/f/software-engineering-architecture/memory-layout-optimizations.md) — Implements memory layout optimizations that reduce garbage collection overhead by disabling reference tracking for non-cyclic structures.
- [Object Schema Definitions](https://awesome-repositories.com/f/software-engineering-architecture/object-schema-definitions.md) — Automates the serialization of complex objects using high-performance data models like structs or dataclasses. ([source](https://msgspec.dev/supported-types.html))
- [Selective Decoding](https://awesome-repositories.com/f/software-engineering-architecture/selective-decoding.md) — Improves decoding speed and reduces memory allocations by ignoring input fields not defined in the schema. ([source](https://msgspec.dev/perf-tips.html))
- [Type Coercion Utilities](https://awesome-repositories.com/f/software-engineering-architecture/type-coercion-utilities.md) — Implements type coercion to convert incompatible types, such as strings to integers, during non-strict decoding. ([source](https://msgspec.dev/supported-types.html))
- [Type Constraint Enforcements](https://awesome-repositories.com/f/software-engineering-architecture/type-constraint-enforcements.md) — Enforces structural and value constraints including numeric ranges, string lengths, and regex patterns. ([source](https://msgspec.dev/api.html))

### Part of an Awesome List

- [Positional Array Encodings](https://awesome-repositories.com/f/awesome-lists/data/serialization-and-encoding/array-serialization/positional-array-encodings.md) — Increases processing speed and reduces payload size by treating data structures as ordered lists without field names. ([source](https://msgspec.dev/structs.html))

### DevOps & Infrastructure

- [Configuration File Loading](https://awesome-repositories.com/f/devops-infrastructure/configuration-management/file-based-configuration/configuration-file-loading.md) — Loads and validates application settings from TOML and YAML files into typed Python structures.
- [Payload Minimization Strategies](https://awesome-repositories.com/f/devops-infrastructure/messaging-infrastructure/payload-minimization-strategies.md) — Decreases the size of encoded messages by omitting default values or stripping field names from objects. ([source](https://msgspec.dev/perf-tips.html))

### Web Development

- [Type-Safe API Schemas](https://awesome-repositories.com/f/web-development/type-safe-api-schemas.md) — Enables the creation of structured data models for consistent API communication and automatic schema generation.
