Skip to main content

Validation

Validator

@dataclass
class Validator()

Base class for validators.

__init__

def __init__(on_fail: Optional[Union[Callable[[Any, FailResult], Any],
OnFailAction]] = None,
**kwargs)

validate

def validate(value: Any, metadata: Dict[str, Any]) -> ValidationResult

Do not override this function, instead implement _validate().

External facing validate function. This function acts as a wrapper for _validate() and is intended to apply any meta- validation requirements, logic, or pre/post processing.

validate_stream

def validate_stream(chunk: Any, metadata: Dict[str, Any],
**kwargs) -> Optional[ValidationResult]

Validates a chunk emitted by an LLM. If the LLM chunk is smaller than the validator's chunking strategy, it will be accumulated until it reaches the desired size. In the meantime, the validator will return None.

If the LLM chunk is larger than the validator's chunking strategy, it will split it into validator-sized chunks and validate each one, returning an array of validation results.

Otherwise, the validator will validate the chunk and return the result.

with_metadata

def with_metadata(metadata: Dict[str, Any])

Assigns metadata to this validator to use during validation.

to_runnable

def to_runnable() -> Runnable

register_validator

def register_validator(
name: str,
data_type: Union[str, List[str]],
has_guardrails_endpoint: bool = False
) -> Callable[[Union[Type[V], Callable]], Union[Type[V], Type[Validator]]]

Register a validator for a data type.

ValidationResult

class ValidationResult(IValidationResult, ArbitraryModel)

ValidationResult is the output type of Validator.validate and the abstract base class for all validation results.

Attributes:

  • outcome str - The outcome of the validation. Must be one of "pass" or "fail".
  • metadata Optional[Dict[str, Any]] - The metadata associated with this validation result.
  • validated_chunk Optional[Any] - The value argument passed to validator.validate or validator.validate_stream.

PassResult

class PassResult(ValidationResult, IPassResult)

PassResult is the output type of Validator.validate when validation succeeds.

Attributes:

  • outcome Literal["pass"] - The outcome of the validation. Must be "pass".
  • value_override Optional[Any] - The value to use as an override if validation passes.

FailResult

class FailResult(ValidationResult, IFailResult)

FailResult is the output type of Validator.validate when validation fails.

Attributes:

  • outcome Literal["fail"] - The outcome of the validation. Must be "fail".
  • error_message str - The error message indicating why validation failed.
  • fix_value Optional[Any] - The auto-fix value that would be applied if the Validator's on_fail method is "fix".
  • error_spans Optional[List[ErrorSpan]] - Segments that caused validation to fail.

ErrorSpan

class ErrorSpan(IErrorSpan, ArbitraryModel)

ErrorSpan provide additional context for why a validation failed. They specify the start and end index of the segment that caused the failure, which can be useful when validating large chunks of text or validating while streaming with different chunking methods.

Attributes:

  • start int - Starting index relative to the validated chunk.
  • end int - Ending index relative to the validated chunk.
  • reason str - Reason validation failed for this chunk.

ValidatorLogs

class ValidatorLogs(IValidatorLog, ArbitraryModel)

Logs for a single validator execution.

Attributes:

  • validator_name str - The class name of the validator
  • registered_name str - The snake_cased id of the validator
  • property_path str - The JSON path to the property being validated
  • value_before_validation Any - The value before validation
  • value_after_validation Optional[Any] - The value after validation; could be different if value_overrides or fixes are applied
  • validation_result Optional[ValidationResult] - The result of the validation
  • start_time Optional[datetime] - The time the validation started
  • end_time Optional[datetime] - The time the validation ended
  • instance_id Optional[int] - The unique id of this instance of the validator

ValidatorReference

class ValidatorReference(IValidatorReference)

ValidatorReference is a serialized reference for constructing a Validator.

Attributes:

  • id Optional[str] - The unique identifier for this Validator. Often the hub id; e.g. guardrails/regex_match. Default None.
  • on Optional[str] - A reference to the property this validator should be applied against. Can be a valid JSON path or a meta-property such as prompt or output. Default None.
  • on_fail Optional[str] - The OnFailAction to apply during validation. Default None.
  • args Optional[List[Any]] - Positional arguments. Default None.
  • kwargs Optional[Dict[str, Any]] - Keyword arguments. Default None.