Overview

msgspec 0.9, released on September 13, 2022, adds validation constraints, JSON Schema generation, and Rich display support.

Main Features

Constraints and JSON Schema

Struct fields support validation constraints (ge, le, pattern) and can automatically generate a JSON Schema.

python
from typing import Annotated
import msgspec
from msgspec import Meta

class Product(msgspec.Struct):
    name: Annotated[str, Meta(min_length=1)]
    price: Annotated[float, Meta(ge=0)]
    quantity: Annotated[int, Meta(ge=0, le=9999)]

schema = msgspec.json.schema(Product)
print(schema)  # JSON Schema dict

Rich support

Structs render with Rich for colorful, structured display in the terminal.

python
import msgspec
from rich import print

class Config(msgspec.Struct):
    host: str = 'localhost'
    port: int = 8000
    debug: bool = False

print(Config())  # colorful Rich display

Sources