API Reference

PyPI version Python

This page documents the core APIs of the llm-contracts library.

Installation

pip install llm-contracts

CLI Commands

llm-validate <file> --schema <schema> [options]

Options

Option Description
--schema <file> Path to the YAML schema file
--html-report <file> Generate HTML report
--md-report <file> Generate Markdown report
--output-format <format> Output format (text/json)
--strict Fail on any validation error
--help Show help message

Python SDK

Validation

from llm_contracts import contracts

# Validate output against schema
result = contracts.validate(
    data,                    # Dict or JSON string
    schema_path,            # Path to YAML schema
)

Parameters

Return Value

Returns a ValidationResult object with the following properties:

Report Generation

# Generate HTML report
contracts.generate_report(
    result,                 # Validation result
    output_path,           # HTML file path
    schema_path,           # Schema file path
    format="html"          # Report format
)

# Generate Markdown report
contracts.generate_report(
    result,                 # Validation result
    output_path,           # Markdown file path
    schema_path,           # Schema file path
    format="markdown"      # Report format
)

Parameters

Combined Validation and Reporting

# Validate and report in one call
result = contracts.validate_and_report(
    data,                  # Data to validate
    schema_path,           # Schema file path
    report_path,           # Optional report file path
    report_format="html"   # Report format
)

Parameters

Schema Format

Basic Schema Structure

schema:
  field_name:
    type: str|int|float|bool|list|dict
    # Other validation properties

rules:
  # Content validation rules
  - rule_type: rule_value

Field Types and Properties

Type Properties
str min_length, max_length, pattern, enum
int min, max, enum
float min, max, enum
bool N/A
list min_items, max_items, items
dict properties, required

Rule Types

Rule Description
keyword_must_include Text must include specified keywords
keyword_must_not_include Text must not include specified keywords
no_placeholder_text Text must not include placeholder patterns
word_count_min Minimum word count
word_count_max Maximum word count
regex_must_match Text must match specified regex pattern
no_duplicate_sentences No duplicate sentences allowed
min_list_items Minimum number of list items
max_passive_voice_ratio Maximum ratio of passive voice
phrase_proximity Maximum distance between specified terms
phrase_order Terms must appear in specified order

For a complete reference, see the API.md file in the GitHub repository.