Skip to main content

A lightweight library for structured output validation in LLM applications

Project description

Seal Validator

PyPI version Python 3.8+ License: MIT

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 features
  • Field: Field definitions with constraints and examples
  • build_format_instructions(): Generate LLM-friendly format instructions

Validation

  • Validator: Validate data against schemas
  • ValidationResult: Structured validation results with error details

Correction Strategies

  • FixPromptStrategy: Generate correction prompts for LLM re-prompting
  • TypeConversionStrategy: Automatic type coercion
  • DefaultValueStrategy: Fill missing values with defaults

LLM Adapters

  • DeepSeekAIAdapter: DeepSeek AI integration
  • LLMAdapter: 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:

  1. Guaranteeing Structure: Ensures outputs match your defined schemas
  2. Auto-Correcting: Automatically fixes common errors without human intervention
  3. Providing Visibility: Complete audit trails for debugging
  4. Being Lightweight: No heavy dependencies, easy to integrate

Made with โค๏ธ for the LLM community

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

seal_validator-0.1.0.tar.gz (35.2 kB view details)

Uploaded Source

Built Distribution

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

seal_validator-0.1.0-py3-none-any.whl (43.2 kB view details)

Uploaded Python 3

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

Hashes for seal_validator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 974a9c1f96addd1dcfdf7d8270b12b87d103d5e235a2d00dc21a461eb7407f4a
MD5 495f3c7e0362ad1bffce2bfa220e06e7
BLAKE2b-256 3c0f1ec3ed584989346716fa4c2d5d5dfc5659243d8e9eab3e1b10dbfd45cd36

See more details on using hashes here.

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

Hashes for seal_validator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd4507751c717ad1c443a74da5a9fa0a7297d720d4bee4a64db4f7bc3747df15
MD5 572d78b4aa9335b18b7f06b57e530d27
BLAKE2b-256 26e61b05e2067006c7fbc1790c3cdd7ac150634220a6984dfea2ed38435cf2a2

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