litestar-org/litestar-skills/msgspec
Claude Code and agent skill by litestar-org/litestar-skills
- community
Checking that the SKILL.md file is still in the repository.
Auto-activate for msgspec, Struct, Meta, msgspec.json, msgspec.msgpack, tagged unions, enc_hook, dec_hook, convert(), or Litestar DTO shapes. Not for Pydantic or ORM models — use their stack-specific skill.
Install
npx vskill@latest install litestar-org/litestar-skills/msgspecSKILL.md
msgspec Skill
msgspec is a high-performance Python library for serialization, deserialization, and typed validation. This guidance targets the 0.22.0 release.
Code Style Rules
- Use PEP 604 for unions:
T | None(notOptional[T]) - `from __future__ import annotations` rule — Library/shared modules that define runtime-introspected
msgspec.Structsubclasses should avoid postponed annotations unless the consuming tool resolves them. Consumer modules that only use Structs MAY use future annotations. - Annotate every serialized field; only annotated attributes become Struct fields
- Use
kw_only=Truefor Structs with more than 2 fields - Put wire-name configuration on
msgspec.field(name=...)or the Struct'srename=
option; msgspec.Meta defines constraints and JSON Schema metadata, not field aliases
Quick Reference
Struct Definition and Options
Struct / defstruct Option | Default | Purpose | ||
|---|---|---|---|---|
kw_only | False | Make all fields keyword-only in __init__ | ||
frozen | False | Immutable instances; adds __hash__ | ||
cache_hash | False | Precompute and cache hash on frozen=True Structs | ||
gc | True | Set False to omit Cyclic GC header on short-lived, non-circular Structs | ||
omit_defaults | False | Omit fields equal to their default (or empty built-in list/dict/set/bytearray default_factory; custom default_factory callables are never omitted) when encoding or calling to_builtins() | ||
repr_omit_defaults | False | Omit fields equal to their default in repr() | ||
forbid_unknown_fields | False | Raise ValidationError on unknown keys during decoding / convert() | ||
rename | None | "lower", "upper", "camel", "pascal", "kebab", `Callable[[str], str \ | None], or Mapping[str, str] (leading _ is preserved for "camel"/"pascal"`) | |
array_like | False | Encode/decode Struct as a positional array ([x, y]) instead of an object | ||
tag / tag_field | None | Discriminated union tag (True, False, str, int, or `Callable[[str], str \ | int]) and discriminator key (defaults to "type" when tag` is set) | |
eq / order | True / False | Generate ==/!= and </<=/>/>= comparison methods | ||
weakref / dict | False / False | Add __weakref__ slot or __dict__ attribute storage (neither dict=True nor weakref=True/inherited __weakref__ can be combined with gc=False in 0.22.0+) |
Partial Updates with UNSET and UnsetType
Use msgspec.UNSET (msgspec.UnsetType) to distinguish omitted fields from explicit null (None). bool(msgspec.UNSET) is False. Fields set to UNSET are automatically omitted when encoding or calling msgspec.to_builtins().
Validation Constraints
Serialization (json, msgpack, yaml, toml) and Raw
dec_hook runs only for unsupported custom annotations. TypeError and ValueError raised by the hook become path-aware ValidationErrors. ValidationError and DecodeError raised by the hook propagate directly without being wrapped in another ValidationError (0.21.1+).
Exception hierarchy: MsgspecError(Exception) is the base class for EncodeError and DecodeError. ValidationError is a subclass of DecodeError (ValidationError -> DecodeError -> MsgspecError). Always catch ValidationError before DecodeError when distinguishing schema validation errors from malformed wire syntax.
Canonical Litestar serializers (match-your-stack)
Litestar apps typically need to_json(value, as_bytes=True) that handles UUID / datetime / Enum / Decimal for Channels broadcasts, log contexts, and JSONB writes. Pick the branch that matches your project.
Branch A — sqlspec is in-stack. Re-export sqlspec's serializer; it already installs an enc_hook covering UUID, datetime, Enum, Decimal, Pydantic, dataclasses, attrs, and msgspec.Struct.
Usage:
Branch B — sqlspec is not in-stack. Use a plain msgspec Encoder; the package natively handles UUID, datetime, date, time, timedelta, Decimal, Enum, dataclasses, attrs classes, and Structs.
Type Coercion with convert() and to_builtins()
from_attributes=False is the default. Plain mappings convert to object-like output types without this option; dataclass, attrs, ORM, and other objects require from_attributes=True. msgspec.Raw instances pass through convert() when the target field/type is msgspec.Raw; to parse a Raw buffer into a Struct, use msgspec.json.decode(raw, type=Struct) (or msgpack.decode).
Struct Helpers (msgspec.structs) and Inspection (msgspec.inspect)
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/msgspec/SKILL.md
Popularity
- Repository stars
- 16
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/litestar-org/litestar-skills/msgspec)