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],
*,
property_path: Optional[str] = "$",
context_vars: Optional[ContextVar[Dict[
str, ContextVar[List[str]]]]] = None,
context: Optional[Context] = None,
**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:
outcomestr - The outcome of the validation. Must be one of "pass" or "fail".metadataOptional[Dict[str, Any]] - The metadata associated with this validation result.validated_chunkOptional[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:
outcomeLiteral["pass"] - The outcome of the validation. Must be "pass".value_overrideOptional[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:
outcomeLiteral["fail"] - The outcome of the validation. Must be "fail".error_messagestr - The error message indicating why validation failed.fix_valueOptional[Any] - The auto-fix value that would be applied if the Validator's on_fail method is "fix".error_spansOptional[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:
startint - Starting index relative to the validated chunk.endint - Ending index relative to the validated chunk.reasonstr - Reason validation failed for this chunk.
ValidatorLogs
class ValidatorLogs(IValidatorLog, ArbitraryModel)
Logs for a single validator execution.
Attributes:
validator_namestr - The class name of the validatorregistered_namestr - The snake_cased id of the validatorproperty_pathstr - The JSON path to the property being validatedvalue_before_validationAny - The value before validationvalue_after_validationOptional[Any] - The value after validation; could be different ifvalue_overrides orfixes are appliedvalidation_resultOptional[ValidationResult] - The result of the validationstart_timeOptional[datetime] - The time the validation startedend_timeOptional[datetime] - The time the validation endedinstance_idOptional[int] - The unique id of this instance of the validator
ValidatorReference
class ValidatorReference(IValidatorReference)
ValidatorReference is a serialized reference for constructing a Validator.
Attributes:
idOptional[str] - The unique identifier for this Validator. Often the hub id; e.g. guardrails/regex_match. Default None.onOptional[str] - A reference to the property this validator should be applied against. Can be a valid JSON path or a meta-property such aspromptoroutput. Default None.on_failOptional[str] - The OnFailAction to apply during validation. Default None.argsOptional[List[Any]] - Positional arguments. Default None.kwargsOptional[Dict[str, Any]] - Keyword arguments. Default None.