Skip to main content

Token-efficient schema language for LLMs with fast Zig backend

Project description

SlimSchema

SlimSchema is a schema language optimized for LLM token efficiency that converts between YAML, Pydantic, and msgspec.

Core Features

  • Token Efficient: Compressed syntax (age: 18..120) saves context window and costs.
  • Deep Validation: Recursive validation for nested objects and dictionaries.
  • Universal Bridge: Convert between formats (Pydantic ↔ YAML ↔ msgspec).

Quick Start

uv pip install slimschema
pip install slimschema

Example

Define a schema with nested types and defaults, validate data, and get a validated Python object.

from slimschema import to_data, to_pydantic

# 1. Define a compact schema (with nested defaults!)
schema = """
user:
  name: str{3..50}
  role: admin | user | guest = "guest"
  settings:
    theme: str = "dark"
    notifications: bool = true
"""

# 2. Validate JSON data (string or dict/list)
# Input missing 'settings' -> defaults apply automatically
data, error = to_data('{"user": {"name": "Alice"}}', schema)

print(data)
# {
#   "user": {
#     "name": "Alice",
#     "role": "guest",
#     "settings": {"theme": "dark", "notifications": True}
#   }
# }

# 3. Convert to Pydantic for your app
UserModel = to_pydantic(schema)
instance = UserModel(**data)
print(instance.user.settings.theme) # "dark"

Full Spec

# Primitives
field: str | int | float | bool | obj

# Default generators
id: str = uuid                 # UUID v4 string
id_v7: str = uuid7             # UUID v7 (time-ordered)
id_ulid: str = ulid            # ULID (sortable)
id_xid: str = xid              # XID (compact, sortable)
id_nano: str = nanoid          # NanoID (URL-safe)
created: datetime = now        # Current datetime
day: date = today              # Current date
ts: int = epoch                # Unix timestamp (seconds)

# Format types
email: email
website: url
birthday: date
timestamp: datetime

# Collections
items: list[str]
tags: set[str]
coords: tuple[float, float]

# Constraints
name: str{3..50}        # Length 3-50
age: 18..120            # Range
code: /^[A-Z]{3}$/      # Regex

# Enums and Unions
status: active | pending | done    # Enum (Literal)
value: str | int                   # Union

# Typed dictionaries
scores: dict[str, int]             # Key-value types

# Inline objects
user: {name: str, age: int, email?: str}

# Optional vs Nullable
?optional_field: str    # Can omit field entirely
nullable_field: str?    # Field required, value can be null
?both: str?             # Optional AND nullable

Default Syntax

count: int = 0
name: str = "default"
items: list = []
created: str = now              # Current datetime
id: str = uuid                  # UUID v4
id2: str = uuid7                # UUID v7 (time-ordered)
id3: str = ulid                 # ULID (sortable)
id4: str = xid                  # XID (compact)
id5: str = nanoid               # NanoID (URL-safe)
ts: int = epoch                 # Unix timestamp (seconds)

API Functions

from slimschema import (
    parse_slimschema,  # YAML string → Schema IR
    to_schema,         # Any input → Schema IR
    to_data,           # JSON string/dict + schema → (data, error)
    to_pydantic,       # Schema → Pydantic model class
    to_msgspec,        # Schema → msgspec Struct class
    to_yaml,           # Schema → YAML string
    SchemaParseError,  # Exception for parse errors
)

Documentation

Guides

Integrations

  • Pydantic: Round-trip conversion and nested models.
  • msgspec: High-performance validation structs.

Reference

API Reference

  • to_data(data, schema): Validate JSON string or dict/list against schema.
  • to_schema(obj): Convert Pydantic/msgspec/YAML to SlimSchema IR.
  • to_yaml(schema): Convert Schema to compact YAML string.
  • to_pydantic(schema): Generate Pydantic models.
  • to_msgspec(schema): Generate msgspec structs.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slimschema-0.0.1.dev0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slimschema-0.0.1.dev0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file slimschema-0.0.1.dev0.tar.gz.

File metadata

  • Download URL: slimschema-0.0.1.dev0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for slimschema-0.0.1.dev0.tar.gz
Algorithm Hash digest
SHA256 1bede5cb81c61f06d884ccd28d40a0d4a173dec005eb1bfd7f5e78a817b29d49
MD5 1dcf486848d852896e3b400f7079ae87
BLAKE2b-256 82023e850eac06dc536b2ae3536de27461d8b62553cdf81210558002101c9c94

See more details on using hashes here.

Provenance

The following attestation bundles were made for slimschema-0.0.1.dev0.tar.gz:

Publisher: on-release-main.yml on botassembly/slimschema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slimschema-0.0.1.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for slimschema-0.0.1.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 308d6af8fedf7e2f1678802be949cf6ecdb2f208396e2491de88d5b272ad43e4
MD5 e33505417ca28d0f5afaec19432d1c92
BLAKE2b-256 43d63ec1b0f7ad3376403bc74c2cfc20eba743fb310063462e610e97f53a13a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for slimschema-0.0.1.dev0-py3-none-any.whl:

Publisher: on-release-main.yml on botassembly/slimschema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page