Skip to main content

Clean, simplified schemas from Pydantic models for LLM prompts - less verbose than JSON Schema

Project description

scheLLMa

Schemas for LLMs and Structured Output

Python 3.11+ PyPI version License: MIT

Converts Pydantic models/JSON Schemas to clean, simplified type definitions perfect for generating structured output with LLM prompts.

Unlike verbose JSON Schema formats, scheLLMa produces readable, concise type definitions that are ideal for language model interactions and structured output generation:

  • Reduce token usage - Concise format saves on API costs
  • Minimize parsing errors - Simple syntax is easier for models to parse, less verbose than JSON Schema, reducing confusion
  • Stay readable - Human-friendly format for prompt engineering

!!! note "" === "Pydantic"

    ```python
    from pydantic import BaseModel, Field
    from schellma import schellma

    class User(BaseModel):
        name: str = Field(default="Anonymous", description="The name of the user")
        age: int = Field(ge=0, le=150, description="User age in years")
        email: str | None = Field(None, examples=["user@example.com"])
    ```

=== "JSON Schema"

    ```json
    {
    "type": "object",
    "properties": {
        "name": { "type": "string", "description": "The name of the user" },
        "age": { "type": "integer" },
        "email": { "type": ["string", "null"], "default": null }
    },
    "required": ["name", "age"],
    "additionalProperties": false
    }
    ```

!!! tip "ScheLLMa"

```typescript
{
    // The name of the user, default: "Anonymous", optional
    "name": string,
    // User age in years, range: 0-150, required
    "age": int,
    // default: null, example: "user@example.com", optional
    "email": string | null,
}
```

Features

  • 🤖 Optimized for LLM prompts - Clean, readable type definitions
  • 💰 Token-efficient - Reduces LLM API costs
  • 🎯 Support for all common Python types (str, int, bool, datetime, etc.)
  • 🏗️ Handle complex nested structures and collections - Strong support for Pydantic model types
  • 🔗 Support for enums, optional types, and unions - Properly extract and display union types
  • ⚙️ Customizable output formatting - Indentation, compact mode, and more
  • 🎨 Rich Default Values - Automatically shows default values in human-readable comments
  • 📏 Smart Constraints - Displays field constraints (length, range, patterns) in clear language
  • Clear Field Status - Explicit required/optional marking
  • 📚 Rich Examples - Inline examples and documentation for better LLM understanding
  • 🔀 Advanced Union Types - Full support for allOf, not constraints, and discriminated unions
  • 🔢 Advanced Arrays - Contains constraints, minContains/maxContains, and enhanced tuple support

Quick Start

from pydantic import BaseModel
from schellma import schellma

class User(BaseModel):
    name: str
    age: int
    email: str | None = None

# Convert to clean schema for LLM prompts
schema = schellma(User)
print(schema)

Output:

{
    "name": string,
    "age": int,
    "email": string | null,
}

View the demo for more examples.

Installation

pip install schellma

Or using uv:

uv add schellma

Install from github

uv add git+https://github.com/andrader/schellma.git

LLM Prompt Integration

from pydantic import BaseModel
from schellma import schellma
import openai

class TaskRequest(BaseModel):
    title: str
    priority: int
    tags: list[str]
    due_date: str | None = None

# Generate schema for LLM prompt
schema = schellma(TaskRequest)


prompt = f"""
Please create a task with the following structure:

{schema}
"""
print(prompt)

# Use with your favorite LLM API
completion = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": prompt
    }]
)

content = completion.choices[0].message.content
print(content)



task = TaskRequest.model_validate_json(clean_content(content))
print(task)
# TaskRequest(title='Task 1', priority=1, tags=['tag1', 'tag2'], due_date=None)

Comparison with JSON Schema

JSON Schema (verbose, token-heavy):

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "email": { "type": ["string", "null"], "default": null }
  },
  "required": ["name", "age"],
  "additionalProperties": false
}

scheLLMa (clean, token-efficient):

{
    "name": string,
    "age": int,
    "email": string | null,
}

Advanced Usage with Type Definitions

from pydantic import BaseModel
from typing import List, Optional
from schellma import schellma

class Address(BaseModel):
    street: str
    city: str
    country: str

class User(BaseModel):
    name: str
    age: int
    addresses: List[Address]
    primary_address: Optional[Address] = None

# Generate with separate type definitions
schema = schellma(User, define_types=True)
print(schema)

Output:

Address {
    "street": string,
    "city": string,
    "country": string,
}

{
    "name": string,
    "age": int,
    "addresses": Address[],
    "primary_address": Address | null,
}

Supported Types

scheLLMa supports a comprehensive range of Python and Pydantic types:

Basic Types

  • strstring
  • intint
  • floatnumber
  • boolboolean

Date/Time Types

  • datetimestring
  • datestring
  • timestring

Collection Types

  • List[T]T[]
  • Set[T]T[]
  • Dict[str, T]{ [key: string]: T }
  • Tuple[T, U][T, U]
  • Tuple[T, ...]T[]

Optional and Union Types

  • Optional[T]T | null
  • Union[T, U]T | U
  • T | NoneT | null

Complex Types

  • Enums → Union of string literals
  • Nested Models → Object types or references
  • UUIDstring
  • Decimalnumber

Examples

Enum Support

from enum import Enum
from pydantic import BaseModel

class Status(Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"

class Task(BaseModel):
    title: str
    status: Status

schema = schellma(Task)
# Output: { "title": string, "status": "active" | "inactive" }

Complex Nested Structures

from pydantic import BaseModel
from typing import Dict, List

class Tag(BaseModel):
    name: str
    color: str

class Post(BaseModel):
    title: str
    content: str
    tags: List[Tag]
    metadata: Dict[str, str]

schema = schellma(Post, define_types=True)

Indentation Control

from schellma import schellma

class User(BaseModel):
    name: str
    age: int

# Default indentation (2 spaces)
result = schellma(User)
# Output:
# {
#   "name": string,
#   "age": int,
# }

# Custom indentation (4 spaces)
result = schellma(User, indent=4)
# Output:
# {
#     "name": string,
#     "age": int,
# }

# No indentation (compact for minimal tokens)
result = schellma(User, indent=False)
# Output: {"name": string,"age": int,}

LLM Integration Examples

OpenAI Integration

import openai
from pydantic import BaseModel
from schellma import schellma

class Response(BaseModel):
    answer: str
    confidence: float
    sources: list[str]

schema = schellma(Response)

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{
        "role": "user",
        "content": f"Please respond with this structure: {schema}"
    }]
)

Anthropic Claude Integration

import anthropic
from schellma import schellma

schema = schellma(MyModel, indent=False)  # Compact for tokens

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-3-sonnet-20240229",
    messages=[{
        "role": "user",
        "content": f"Format your response as: {schema}"
    }]
)

Development

Setup

git clone https://github.com/andrader/schellma.git
cd schellma
uv sync --dev

Running Tests

uv run python -m pytest

Type Checking

uv run mypy src/schellma/

Linting

uv run ruff check src/schellma/
uv run ruff format src/schellma/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Guidelines

  1. Follow the existing code style (enforced by ruff)
  2. Add tests for any new functionality
  3. Update documentation as needed
  4. Ensure all tests pass and type checking succeeds

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See Changelog for the changelog.

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

schellma-0.3.0.tar.gz (99.6 kB view details)

Uploaded Source

Built Distribution

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

schellma-0.3.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file schellma-0.3.0.tar.gz.

File metadata

  • Download URL: schellma-0.3.0.tar.gz
  • Upload date:
  • Size: 99.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for schellma-0.3.0.tar.gz
Algorithm Hash digest
SHA256 04d08336b4e575c1f0e65d5b292f634b205e964dff27ffe54dc1e67eca0d95e7
MD5 1c78eb2faa06547e7fe2018482569a0e
BLAKE2b-256 1a4559a0aeb9b447150dd8bdd2d9b6f799e904b9ec43c2fb3466f5741901e873

See more details on using hashes here.

File details

Details for the file schellma-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: schellma-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for schellma-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b21bf8a70f32099a11a22d63d47fe39cd422fb225f8a6f24011c551b30441748
MD5 961459eaffc320e027c3f925f81c7263
BLAKE2b-256 4106eb39927b43317c89ca2e3095d625e850d0171887d7277a60b76ea1ae74be

See more details on using hashes here.

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