Implementation:Explodinggradients Ragas MetricValidators Module
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Metrics, Validation |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The validators module provides mixin classes and helper functions for validating metric result values against type-specific constraints such as discrete allowed values, numeric ranges, and ranked list lengths.
Description
The module defines an abstract BaseValidator with a validate_result_value method, and three concrete validator mixins: DiscreteValidator checks that a result value is among a list of allowed strings; NumericValidator verifies that a numeric result falls within a tuple range or Python range object; RankingValidator ensures that a list result has the expected length specified by an integer. Two helper functions are provided: get_validator_for_allowed_values returns the appropriate validator class based on the type of the allowed_values parameter, and get_metric_type_name returns a human-readable metric type name string.
Usage
These validators are used as mixins in metric class hierarchies. The allowed_values attribute is inherited from the metric base class and determines which validator behavior applies. Call validate_result_value() on a metric instance to check whether a result is valid.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/metrics/validators.py L19-125
|
| Classes | BaseValidator(ABC), DiscreteValidator, NumericValidator, RankingValidator
|
| Functions | get_validator_for_allowed_values(allowed_values) -> Type[BaseValidator], get_metric_type_name(allowed_values) -> str
|
| Import | from ragas.metrics.validators import NumericValidator, DiscreteValidator
|
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
result_value (DiscreteValidator) |
Any |
Value to check against allowed_values: List[str]
|
result_value (NumericValidator) |
Any |
Value to check against allowed_values: Tuple[float, float] or range
|
result_value (RankingValidator) |
Any |
Value to check against allowed_values: int (expected list length)
|
allowed_values (helper) |
AllowedValuesType |
Union of List[str], Tuple[float, float], range, or int |
Outputs
| Output | Type | Description |
|---|---|---|
validate_result_value() |
Optional[str] |
Error message string if validation fails, None if valid
|
get_validator_for_allowed_values() |
Type[BaseValidator] |
The appropriate validator class |
get_metric_type_name() |
str |
One of "DiscreteMetric", "NumericMetric", "RankingMetric", or "CustomMetric" |
Usage Examples
from ragas.metrics.validators import (
DiscreteValidator, NumericValidator, RankingValidator,
get_validator_for_allowed_values, get_metric_type_name,
)
# Determine validator type from allowed_values
validator_cls = get_validator_for_allowed_values(["yes", "no"])
print(validator_cls) # <class 'DiscreteValidator'>
print(get_metric_type_name((0.0, 1.0))) # "NumericMetric"
print(get_metric_type_name(3)) # "RankingMetric"
Related Pages
- RankingMetric_Class - Uses RankingValidator as a mixin
- MetricResult_Class - Values validated by these validators
- Validation_Module - Dataset-level validation utilities