LLM-ify your JSON schemas
Project description
llm-schema-lite
Transform verbose Pydantic JSON schemas into LLM-friendly formats. Reduce token usage by 60-85% while preserving essential type information.
🚀 Quick Start
Basic Usage
from pydantic import BaseModel
from llm_schema_lite import simplify_schema
# Define your Pydantic model
class User(BaseModel):
name: str
age: int
email: str
# Transform to LLM-friendly format
schema = simplify_schema(User)
print(schema.to_string())
# Output: { name: string, age: int, email: string }
Multiple Output Formats
# JSONish format (BAML-like) - Default
schema = simplify_schema(User)
print(schema.to_string())
# { name: string, age: int, email: string }
# TypeScript format
schema_ts = simplify_schema(User, format_type="typescript")
print(schema_ts.to_string())
# interface User { name: string; age: int; email: string; }
# YAML format
schema_yaml = simplify_schema(User, format_type="yaml")
print(schema_yaml.to_string())
# name: string
# age: int
# email: string
Advanced Features
from pydantic import BaseModel, Field
class Product(BaseModel):
name: str = Field(..., description="Product name", min_length=1)
price: float = Field(..., ge=0, description="Price must be positive")
tags: list[str] = Field(default_factory=list)
# Include metadata (descriptions, constraints)
schema_with_meta = simplify_schema(Product, include_metadata=True)
print(schema_with_meta.to_string())
# {
# name: string //Product name, minLength: 1,
# price: float //Price must be positive, min: 0,
# tags: string[]
# }
# Exclude metadata for minimal output
schema_minimal = simplify_schema(Product, include_metadata=False)
print(schema_minimal.to_string())
# {
# name: string,
# price: float,
# tags: string[]
# }
Nested Models
class Address(BaseModel):
street: str
city: str
zipcode: str
class Customer(BaseModel):
name: str
email: str
address: Address
schema = simplify_schema(Customer)
print(schema.to_string())
# { name: string, email: string, address: { street: string, city: string, zipcode: string } }
Different Output Methods
schema = simplify_schema(User)
# String output
print(schema.to_string())
# JSON output
print(schema.to_json(indent=2))
# Dictionary output
print(schema.to_dict())
# YAML output (if format_type="yaml")
print(schema.to_yaml())
📊 Token Reduction
Compare the token usage:
import json
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
email: str
# Original Pydantic schema (verbose)
original_schema = User.model_json_schema()
print("Original tokens:", len(json.dumps(original_schema)))
# Simplified schema (LLM-friendly)
simplified = simplify_schema(User)
print("Simplified tokens:", len(simplified.to_string()))
# Typical reduction: 60-85% fewer tokens!
🎯 Use Cases
- LLM Function Calling: Reduce schema tokens in function definitions
- DSPy: Optimize schema definitions for better performance
- LangChain: Streamline Pydantic model schemas
- Raw LLM APIs: Minimize prompt overhead with concise schemas
Installation
You can install llm-schema-lite using pip:
pip install llm-schema-lite
Or using uv:
uv pip install llm-schema-lite
Development
This project uses uv for package management and includes pre-commit hooks for code quality.
Setup Development Environment
- Install uv if you haven't already:
curl -LsSf https://astral.sh/uv/install.sh | sh
- Quick setup with Make:
make setup
Or manually:
# Create virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install package with dev dependencies
uv pip install -e ".[dev]"
# Install pre-commit hooks
uv pip install pre-commit
pre-commit install
pre-commit install --hook-type commit-msg
Available Make Commands
Run make help to see all available commands:
make install- Install packagemake install-dev- Install with dev dependenciesmake test- Run testsmake test-cov- Run tests with coveragemake test-parallel- Run tests in parallel (faster)make test-fast- Run tests excluding slow onesmake lint- Run all lintersmake format- Format codemake build- Build packagemake changelog- Generate changelogmake clean- Clean build artifacts
Running Tests
make test
# or
pytest
Code Quality
The project uses several tools to maintain code quality:
- Ruff: Fast Python linter and formatter (replaces flake8, isort, and more)
- MyPy: Static type checker for type safety
- Bandit: Security vulnerability scanner
- Pre-commit: Git hooks for automated checks
- Pytest: Testing framework with coverage reporting
# Format code
make format
# Run linters
make lint
# Run pre-commit on all files
make pre-commit-run
# Run tests in parallel (faster for large test suites)
make test-parallel
Changelog Management
This project uses git-changelog with conventional commits:
# Generate changelog
make changelog
Commit message format:
feat:- New featuresfix:- Bug fixesdocs:- Documentation changesrefactor:- Code refactoringtest:- Test changeschore:- Maintenance tasksperf:- Performance improvements
Building and Publishing
Build the package
uv build
Publish to PyPI
# Install twine if needed
uv pip install twine
# Upload to PyPI
twine upload dist/*
Publish to TestPyPI (for testing)
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
License
See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_schema_lite-0.2.1.tar.gz.
File metadata
- Download URL: llm_schema_lite-0.2.1.tar.gz
- Upload date:
- Size: 103.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96cad38053694f1334cb37cc6065cdb028da45ea98127070f0fa3b396ce97ddd
|
|
| MD5 |
9652206fb2223f60d8447465d5a2ff6c
|
|
| BLAKE2b-256 |
87a0b5a0f30b3c1369b3fb7c62ed95f041322d5bfbd947e92a3a7c0e179cb0c1
|
Provenance
The following attestation bundles were made for llm_schema_lite-0.2.1.tar.gz:
Publisher:
publish.yaml on rohitgarud/llm-schema-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_schema_lite-0.2.1.tar.gz -
Subject digest:
96cad38053694f1334cb37cc6065cdb028da45ea98127070f0fa3b396ce97ddd - Sigstore transparency entry: 604834835
- Sigstore integration time:
-
Permalink:
rohitgarud/llm-schema-lite@7fc815168558a39b4f083149d491a9dd945307f1 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/rohitgarud
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7fc815168558a39b4f083149d491a9dd945307f1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file llm_schema_lite-0.2.1-py3-none-any.whl.
File metadata
- Download URL: llm_schema_lite-0.2.1-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63a5a22a2c2c95c733e3d929a16ef56e8b4db686095fbe18e171bc488e15fe96
|
|
| MD5 |
fec21128970096c3eae67f49053d0a8c
|
|
| BLAKE2b-256 |
83b24d89d586366342ac3a43d54244493b1994047cdef428cd55da66c52d2493
|
Provenance
The following attestation bundles were made for llm_schema_lite-0.2.1-py3-none-any.whl:
Publisher:
publish.yaml on rohitgarud/llm-schema-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_schema_lite-0.2.1-py3-none-any.whl -
Subject digest:
63a5a22a2c2c95c733e3d929a16ef56e8b4db686095fbe18e171bc488e15fe96 - Sigstore transparency entry: 604834836
- Sigstore integration time:
-
Permalink:
rohitgarud/llm-schema-lite@7fc815168558a39b4f083149d491a9dd945307f1 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/rohitgarud
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@7fc815168558a39b4f083149d491a9dd945307f1 -
Trigger Event:
release
-
Statement type: