msgspec

Define msgspec Structs, validation constraints, tagged unions, and JSON/MessagePack codecs in Python.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill msgspec-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: msgspec
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/msgspec
Command: npx skills add https://github.com/renjianguo666/litecms --skill msgspec-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires msgspec, and includes references (resource) components.

What problem does it solve? Writing high-performance serialization and validation code with msgspec requires knowing its specific patterns: Struct options, Meta constraints, tagged unions, encoder/decoder singletons, and hooks for custom types. This Skill provides those patterns so generated code follows msgspec best practices instead of falling back to slower Pydantic or dataclass habits. ## Core Features & Use Cases - Struct Definition & Validation: Generate msgspec.Struct models with Annotated Meta constraints (gt, ge, min_length, pattern), kw_only, frozen, and forbid_unknown_fields options. - Serialization Codecs: Set up cached JSON and MessagePack Encoder/Decoder singletons with enc_hook/dec_hook for datetime, UUID, Decimal, and Enum types. - Tagged Unions & Conversion: Build discriminated unions with custom tag fields and values, plus strict/lax type coercion via msgspec.convert(). - Use Case: When building a Litestar API, use this Skill to define camelCase response Structs, a to_json serializer (re-exporting sqlspec's when available), and a tagged-union event system with round-trip encode/decode validation. ## Quick Start Define a msgspec Struct for a user API response with validation constraints and a JSON encoder that handles UUID and datetime fields.

Frequently Asked Questions about msgspec

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define a msgspec Struct with validation constraints?

Annotate fields with Annotated[Type, Meta(...)] using parameters like gt, ge, min_length, max_length, and pattern. Constraints are checked at decode time with zero runtime overhead, and reusable aliases like PositiveInt can be defined at module level.

msgspec vs Pydantic: which should I use for serialization?

msgspec Structs are roughly 5x more memory-efficient and serialize faster than Pydantic, making them ideal for response shapes and internal messaging. Use Pydantic only when you need validate_assignment, complex field validators, or its ecosystem tooling.

How do I handle datetime and UUID in msgspec JSON encoding?

Pass enc_hook and dec_hook functions to msgspec.json.Encoder and Decoder. The enc_hook converts datetime to ISO strings and UUID to str; the dec_hook reverses this during decoding. If sqlspec is in your stack, re-export its to_json which includes these hooks.

Can I use from __future__ import annotations with msgspec Structs?

Modules that define msgspec.Struct subclasses must not use it, because runtime introspection breaks. Consumer modules that only use Structs (handlers, services, tests) may and typically should use future annotations.

How do tagged unions work in msgspec?

Add tag=True or tag="value" to each Struct variant, optionally with a custom tag_field name. Decoding against the union type inspects the tag field and routes to the matching Struct. All variants must share the same tag_field and have unique tag values.

Why does msgspec.decode raise ValidationError on string IDs?

Strict mode (the default) rejects type mismatches like a string where an int is expected. Use msgspec.convert(data, Type, strict=False) for lax coercion at trust boundaries such as legacy dict inputs.