API Reference

Main Validation Class

Configuration

Validation Report

class AisertReport(status: bool, rules: dict)[source]

Final validation report containing overall status and detailed results.

Returned by Aisert.collect to provide comprehensive validation outcomes. Contains both high-level pass/fail status and granular per-validation details.

Parameters:
  • status (bool) – True if all validations passed, False if any failed

  • rules (dict) – Dictionary mapping execution order to validation results

The rules dictionary has the format:

{1: {'validator': 'ContainsValidator', 'status': True, 'reason': '...'}}

Example usage:

report = Aisert("Hello world").assert_contains(["Hello"]).collect()
if report.status:
    print("All validations passed!")
for order, result in report.rules.items():
    print(f"{order}: {result['validator']} - {result['status']}")

Added in version 0.1.0.

__init__(status: bool, rules: dict)[source]

Create a new validation report.

Parameters:
  • status (bool) – Overall validation status (True if all passed)

  • rules (dict) – Dictionary of validation results keyed by execution order

Exceptions

exception AisertError[source]

Base exception for all Aisert errors.

exception SchemaValidationError[source]

Schema validation specific errors.

exception ContainsValidationError[source]

Text contain validation errors

exception TokenValidationError[source]

Token counting specific errors.

exception SemanticValidationError[source]

Semantic validation specific errors.

Custom Validator Base Classes

Base classes for creating custom validators.

Token Validator Base

class TokenValidatorBase[source]

Abstract base class for token counting implementations across different AI providers.

Provides a common interface for token counting while allowing provider-specific implementations (OpenAI, Anthropic, HuggingFace, Google). Each provider has different tokenization methods and APIs.

Subclasses must implement: - get_instance(): Factory method for singleton pattern - count(): Token counting logic for the specific provider

Example

class MyTokenValidator(TokenValidatorBase):
def count(self, text: str) -> int:

return len(text.split()) # Simple word count

__init__()[source]

Initialize base token validator.

classmethod get_instance(**kwargs)[source]

Factory method to get validator instance (typically singleton).

Should be implemented by subclasses to return cached instances for performance, as tokenizers can be expensive to initialize.

Parameters:

**kwargs – Provider-specific configuration parameters

Returns:

Instance of the token validator

Raises:

NotImplementedError – If subclass doesn’t implement this method

count(text: str) int[source]

Count tokens in the provided text using provider-specific logic.

Parameters:

text – Input text to count tokens for

Returns:

Number of tokens in the text

Raises:

NotImplementedError – If subclass doesn’t implement this method

Example

validator = OpenAITokenValidator.get_instance(token_model=”gpt-4”) count = validator.count(“Hello world”) # Returns token count

Semantic Validator Base

Validator Factories

Factories for registering and managing custom validators.

Token Validator Factory

class TokenValidatorFactory[source]

Factory class for creating instances of token validators based on the model provider. This class provides a method to get an instance of a specific token validator based on the model provider and token model.

classmethod register_token_validator(model_provider: str, token_validator_class)[source]

A class method ot register custom token validators :param model_provider: provider of the model :param token_validator_class: custom token validator class

static get_instance(model_provider: str, token_model: str = None, token_encoding: str = None, api_key: str = None, **kwargs)[source]

Get an instance of TokenValidator with the specified provider and model. :param model_provider: Provider name :param token_model: Model name :param token_encoding: Encoding method :param api_key: API key if needed :return: An instance of TokenValidator.

Semantic Validator Factory