Implementation:Explodinggradients Ragas MetricResult Class
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Metrics, Data_Model |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The MetricResult class is a transparent wrapper around metric evaluation results that supports arithmetic operations, comparisons, iteration, and serialization while preserving access to metadata such as reasoning and traces.
Description
MetricResult stores an underlying value of any type along with optional reason (string) and traces (dictionary with "input"/"output" keys) metadata. The class implements Python's numeric dunder methods (__add__, __sub__, __mul__, __truediv__ and their reverse variants) for numeric results, container protocols (__getitem__, __iter__, __len__) for list results from ranking metrics, and type conversion methods (__float__, __int__) for numeric results. Comparison operators work across all value types. The __getattr__ fallback forwards attribute access to the underlying value, wrapping returned values of the same type back in MetricResult to preserve metadata. Pydantic integration is provided through __get_pydantic_core_schema__ for validation and serialization.
Usage
MetricResult instances are typically created by metric scoring methods. Use the .value property to access the raw result, .reason for the LLM reasoning, and .to_dict() or .__json__() for serialization.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/metrics/result.py L11-241
|
| Class Signature | class MetricResult
|
| Constructor | def __init__(self, value: Any, reason: Optional[str] = None, traces: Optional[Dict[str, Any]] = None)
|
| Import | from ragas.metrics.result import MetricResult
|
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
value |
Any |
The raw metric result (float, str, list, etc.) |
reason |
Optional[str] |
Optional reasoning text from the LLM |
traces |
Optional[Dict[str, Any]] |
Optional traces dict; only "input" and "output" keys are allowed |
Outputs
| Property/Method | Type | Description |
|---|---|---|
.value |
Any |
The raw underlying result value |
.reason |
Optional[str] |
The reasoning metadata |
.to_dict() |
Dict |
Dictionary with "result" and "reason" keys |
.__json__() |
Dict |
Dictionary with "value" and "reason" keys for JSON serialization |
Usage Examples
from ragas.metrics.result import MetricResult
# Numeric result with arithmetic
result = MetricResult(value=0.85, reason="High faithfulness")
print(float(result)) # 0.85
print(result + 0.1) # 0.95
print(result > 0.5) # True
# List result for ranking metrics
ranking = MetricResult(value=["doc_a", "doc_b", "doc_c"])
print(len(ranking)) # 3
print(ranking[0]) # 'doc_a'
for item in ranking:
print(item)
# Serialization
print(result.to_dict()) # {'result': 0.85, 'reason': 'High faithfulness'}
Related Pages
- RankingMetric_Class - Produces list-valued MetricResult instances
- MetricValidators_Module - Validates MetricResult values against constraints
- Utils_Module - safe_nanmean works with MetricResult float values