A lightweight library for structured output validation in LLM applications
Project description
Seal Validator
Seal Validator is a lightweight Python library for structured output validation in LLM applications. It ensures your LLM outputs conform to defined schemas with automatic correction and retry mechanisms.
๐ Features
- ๐ Schema Validation: Pydantic-based schema definitions with comprehensive validation
- ๐ Auto-Correction: Multiple correction strategies (fix prompts, type conversion, default values)
- ๐ค LLM Integration: Built-in adapters for popular LLM providers (DeepSeek, OpenAI-compatible)
- ๐ Type Safety: Full type hints and generic support
- โก Lightweight: Minimal dependencies, focused on core functionality
- ๐ Audit Trail: Complete execution logging for debugging and monitoring
๐ฆ Installation
pip install seal-validator
๐ Quick Start
1. Define Your Schema
from seal import SealModel, Field
from typing import List, Optional
class UserProfile(SealModel):
"""User profile schema with validation constraints."""
name: str = Field(..., min_length=1, max_length=50)
age: int = Field(..., ge=0, le=150)
email: Optional[str] = Field(None, pattern=r'^[\w\.-]+@[\w\.-]+\.\w+$')
interests: List[str] = Field(default_factory=list)
2. Validate Data
from seal import Validator
validator = Validator(UserProfile)
# Valid data
result = validator.validate({
'name': 'Alice Johnson',
'age': 28,
'email': 'alice@example.com',
'interests': ['reading', 'hiking']
})
print(result.is_valid) # True
# Invalid data
result = validator.validate({
'name': '', # Empty name
'age': 200, # Age too high
'email': 'invalid-email'
})
print(result.is_valid) # False
print(result.errors) # List of validation errors
3. Generate LLM Format Instructions
from seal import build_format_instructions
instructions = build_format_instructions(UserProfile)
print(instructions)
Output:
{
"name": "string (required, min_length: 1, max_length: 50)",
"age": "integer (required, ge: 0, le: 150)",
"email": "string or null (optional, pattern: regex)",
"interests": "array of string (default: [])"
}
4. Full Automation with SealEngine
from seal import SealEngine, DeepSeekAIAdapter, DeepSeekConfig
from seal import JsonParser, Validator, FixPromptStrategy
# Configure LLM adapter
config = DeepSeekConfig(api_key="your-api-key", model="deepseek-chat")
llm_adapter = DeepSeekAIAdapter(config)
# Setup engine components
parser = JsonParser()
validator = Validator(UserProfile)
corrector = FixPromptStrategy(max_retries=3)
# Create engine
engine = SealEngine[UserProfile](
model=UserProfile,
llm_adapter=llm_adapter,
parser=parser,
validator=validator,
correctors=[corrector]
)
# Generate structured output
result = engine.run_sync("Create a user profile for a software developer named Alex, age 30")
if result.success:
user = result.data
print(f"Name: {user.name}, Age: {user.age}")
else:
print(f"Failed: {result.errors}")
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SealEngine โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ Prompt โโ โ LLM โโ โ Parser โโ โ Validatorโ โ
โ โ Builder โ โ Adapter โ โ (JSON) โ โ โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโฌโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโ โ
โ โ Corrector โ โ
โ โ (if invalid)โ โ
โ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Core Components
Schema Definition
SealModel: Pydantic-based model with extended featuresField: Field definitions with constraints and examplesbuild_format_instructions(): Generate LLM-friendly format instructions
Validation
Validator: Validate data against schemasValidationResult: Structured validation results with error details
Correction Strategies
FixPromptStrategy: Generate correction prompts for LLM re-promptingTypeConversionStrategy: Automatic type coercionDefaultValueStrategy: Fill missing values with defaults
LLM Adapters
DeepSeekAIAdapter: DeepSeek AI integrationLLMAdapter: Base class for custom adapters
Engine
SealEngine: Orchestrates the entire validation pipeline- Supports sync and async operations
- Configurable retry logic
๐งช Testing
# Run all tests
python -m pytest tests/
# Run with coverage
python -m pytest --cov=seal tests/
# Run specific module tests
python -m pytest tests/codes/validation/
๐ Examples
See the demo/quick_start.py file for a comprehensive example.
๐ค Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Links
๐ก Why Seal?
LLMs are powerful but their outputs can be unpredictable. Seal bridges the gap between LLM creativity and application requirements by:
- Guaranteeing Structure: Ensures outputs match your defined schemas
- Auto-Correcting: Automatically fixes common errors without human intervention
- Providing Visibility: Complete audit trails for debugging
- Being Lightweight: No heavy dependencies, easy to integrate
Made with โค๏ธ for the LLM community
Project details
Release history Release notifications | RSS feed
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 seal_validator-0.1.0.tar.gz.
File metadata
- Download URL: seal_validator-0.1.0.tar.gz
- Upload date:
- Size: 35.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
974a9c1f96addd1dcfdf7d8270b12b87d103d5e235a2d00dc21a461eb7407f4a
|
|
| MD5 |
495f3c7e0362ad1bffce2bfa220e06e7
|
|
| BLAKE2b-256 |
3c0f1ec3ed584989346716fa4c2d5d5dfc5659243d8e9eab3e1b10dbfd45cd36
|
File details
Details for the file seal_validator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: seal_validator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 43.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd4507751c717ad1c443a74da5a9fa0a7297d720d4bee4a64db4f7bc3747df15
|
|
| MD5 |
572d78b4aa9335b18b7f06b57e530d27
|
|
| BLAKE2b-256 |
26e61b05e2067006c7fbc1790c3cdd7ac150634220a6984dfea2ed38435cf2a2
|