SchemaChat
A unified Python interface for multiple LLM chat providers with structured output support using Pydantic models.
Features
- Multi-provider support: OpenAI, Ollama, and OpenRouter APIs
- Structured output: Generate validated responses using Pydantic models
- Factory pattern: Easy provider instantiation and switching
- Advanced context management: Intelligent context size optimization for Ollama
- Fallback model support: Switch models dynamically for error recovery
- Type safety: Full type hints and validation throughout
Installation
pip install schemachat
Quick Start
Basic Text Generation
from schemachat.core.configs.openai import OpenAIConfig
from schemachat.providers.factory import ChatProviderFactory
# Configure OpenAI provider
config = OpenAIConfig(
api_key="your-openai-api-key",
base_url="https://api.openai.com/v1",
model_name="gpt-4",
fallback_model_name="gpt-3.5-turbo"
)
# Create provider instance
client = ChatProviderFactory.create_provider(config)
# Generate response
response = client.invoke("Hello, world!")
print(response)
Structured Output Generation
from pydantic import BaseModel
from typing import List
class Person(BaseModel):
name: str
age: int
skills: List[str]
# Generate structured response
person = client.invoke_structured(
"Generate a profile for a Python developer",
response_model=Person
)
print(f"Name: {person.name}")
print(f"Age: {person.age}")
print(f"Skills: {', '.join(person.skills)}")
Using Ollama Provider
from schemachat.core.configs.ollama import OllamaConfig
# Configure Ollama provider
config = OllamaConfig(
base_url="http://localhost:11434",
model_name="llama3.1",
fallback_model_name="llama3",
max_num_ctx=128, # Context size in KB
num_predict=8192 # Max prediction tokens
)
client = ChatProviderFactory.create_provider(config)
response = client.invoke("Explain quantum computing")
Supported Providers
OpenAI (and OpenAI-compatible APIs)
- Official OpenAI API
- OpenRouter
- Any OpenAI-compatible endpoint
Ollama
- Local Ollama installations
- Advanced context size management
- Automatic token calculation and optimization
Architecture
SchemaChat uses a factory pattern with provider-specific implementations:
BaseLLMClient: Abstract interface for all providersBaseConfig: Configuration base class with validationChatProviderFactory: Factory for creating provider instances- Provider-specific optimizations: Each provider includes tailored optimizations
Configuration Options
OpenAI Configuration
OpenAIConfig(
api_key="your-key",
base_url="https://api.openai.com/v1",
model_name="gpt-4",
fallback_model_name="gpt-3.5-turbo",
max_tokens=8192,
temperature=0.7,
top_p=0.9
)
Ollama Configuration
OllamaConfig(
base_url="http://localhost:11434",
model_name="llama3.1",
fallback_model_name="llama3",
max_num_ctx=128, # KB
num_ctx=32768, # Specific context size
num_predict=8192 # Max prediction tokens
)
Advanced Features
Provider Information
info = client.get_provider_info()
print(f"Provider: {info['provider_type']}")
print(f"Model: {info['model_name']}")
print(f"Base URL: {info['base_url']}")
Model Switching
# Switch to fallback model
client.use_fallback_model()
# Switch to specific model
client.use_fallback_model("gpt-4-turbo")
Error Handling
try:
response = client.invoke("Your prompt")
except Exception as e:
print(f"Error: {e}")
# Automatically try fallback model
client.use_fallback_model()
response = client.invoke("Your prompt")
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/yourusername/schemachat.git
cd schemachat
# Install dependencies
uv sync
# Install development dependencies
uv sync --group dev
Running Tests
uv run pytest
Code Formatting
uv run black .
uv run ruff check .
Type Checking
uv run mypy .
Requirements
- Python 3.10+
- pydantic>=2.12.0
- openai>=2.3.0 (for OpenAI providers)
- ollama>=0.6.0 (for Ollama providers)
- tiktoken>=0.12.0 (for token counting)
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for schemachat 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| schemachat-0.1.1.tar.gz | 77.4 kB | Without provenance | Details for schemachat-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| schemachat-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for schemachat-0.1.1-py3-none-any.whl |
Total release size: 91.6 kB
Release files / schemachat-0.1.1.tar.gz
| Download URL | schemachat-0.1.1.tar.gz |
|---|---|
| Size | 77.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
451c6cf98b143687e8d54252b87ee9cd1e692dcf3e16d1ff23c1253314597eff
|
|
BLAKE2b-256 checksum How to use checksums |
6bacd4e497818c5bcbe59896bda46257b6c5e15570265e86107b937ea82de422
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.18
|
Release files / schemachat-0.1.1-py3-none-any.whl
| Download URL | schemachat-0.1.1-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
02ba890cbafd2e03eb98a1d52ddef5fe69213f34cb37ccf14ac14e87e2e51cb4
|
|
BLAKE2b-256 checksum How to use checksums |
e9bd8823f8f843f58158b13df79b665e5925ccb560e711cc46c05ca5d5b7ac50
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.18
|
Security for schemachat 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.