msgspec-patterns

Review msgspec Struct usage for performance, correctness, and memory efficiency.

20|27|Updated Oct 30, 2025
One-click install
npx skills add https://github.com/mlcommons/endpoints --skill msgspec-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: msgspec-patterns
Source: https://github.com/mlcommons/endpoints/tree/main/.claude/skills/msgspec-patterns
Command: npx skills add https://github.com/mlcommons/endpoints --skill msgspec-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you design, review, and optimize msgspec-based data models so Python serialization code is faster, cleaner, and safer to maintain.

Core Features & Use Cases

  • Structured Data Modeling: Prefer msgspec.Struct for schema-driven objects instead of dicts or dataclasses.
  • Encoding and Decoding Performance: Apply options like omit_defaults, array_like, and tagged unions to reduce payload size and improve speed.
  • Safety Analysis: Evaluate whether gc=False is safe by checking for reference cycles, mutable containers, and back-references.
  • Practical Use Cases: Use it when defining API payloads, reviewing configuration schemas, or auditing existing structs for performance regressions and memory risks.

Quick Start

Ask the Skill to review a msgspec Struct definition and tell you whether gc=False is safe and which performance options to apply.

Frequently Asked Questions about msgspec-patterns

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

FAQPage Schema
How do I optimize msgspec Struct usage for faster serialization in Python?

Optimize msgspec Struct serialization by applying performance options like omit_defaults, array_like, and tagged unions to reduce payload size and improve encoding and decoding speed.

When is it safe to use gc=False in msgspec Structs?

Using gc=False in msgspec Structs is safe only when the struct avoids reference cycles, mutable containers, and back-references, preventing memory leaks and unsafe garbage collection behavior.

Should I use msgspec Struct instead of Python dataclasses or dicts for data models?

Prefer msgspec Struct over dataclasses or dicts for schema-driven Python objects because it provides faster serialization, improved correctness, and better memory efficiency for configuration schemas and API payloads.

What is the best way to audit existing msgspec structs for memory risks?

Audit existing msgspec structs for memory risks by evaluating struct options, checking for reference cycles, and analyzing decoding behavior to identify potential performance regressions and unsafe garbage collection patterns.

How do I reduce msgspec payload size for API responses?

Reduce msgspec payload size for API responses by applying the omit_defaults option to exclude default values and using tagged unions to efficiently encode variant data structures during serialization.

What are the limitations of using gc=False with mutable containers in msgspec?

Using gc=False with mutable containers in msgspec introduces reference cycle risks that the garbage collector cannot manage, potentially causing memory leaks and unsafe memory behavior during deserialization.