Implementation:Evidentlyai Evidently Legacy Custom Value Metric
| Knowledge Sources | |
|---|---|
| Domains | ML Monitoring, Custom Metrics |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
CustomValueMetric is a metric class that allows users to define custom scalar metrics by providing a callable function that computes a float value from the input data.
Description
The module defines two main classes and a renderer:
CustomCallableMetricResult (extends MetricResult):
- A simple result container with a single
value: floatfield - Registered with type alias
"evidently:metric_result:CustomCallableMetricResult"
CustomValueMetric (extends Metric[CustomCallableMetricResult]):
- Accepts a callable function (or function reference string) that takes an
InputDataobject and returns afloat - Supports an optional
titlefor display purposes and an optionalsizefor widget rendering - When constructed with a callable, it stores both the callable (as a private attribute
_func) and its qualified name as a string infunc - When constructed with a string, it stores the string but sets
_functoNone(callingcalculatewill raise aValueErrorin this case) - Registered with type alias
"evidently:metric:CustomValueMetric"
CustomValueMetricRenderer (extends MetricRenderer):
- Renders the metric result as an HTML counter widget
- Displays the float value with 2 decimal places
- Supports
HALForFULLwidget size
The CustomCallableType type alias is defined as Callable[[InputData], float].
Usage
Use this metric when you need to compute a custom scalar value from your data that is not covered by built-in metrics. It is ideal for one-off custom calculations, business-specific KPIs, or experimental metrics during development.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File:
src/evidently/legacy/metrics/custom_metric.py
Signature
class CustomCallableMetricResult(MetricResult):
class Config:
type_alias = "evidently:metric_result:CustomCallableMetricResult"
value: float
CustomCallableType = Callable[[InputData], float]
class CustomValueMetric(Metric[CustomCallableMetricResult]):
class Config:
type_alias = "evidently:metric:CustomValueMetric"
func: str
title: Optional[str] = None
size: Optional[WidgetSize] = None
def __init__(
self,
func: Union[CustomCallableType, str],
title: str = None,
size: Optional[WidgetSize] = None,
options: AnyOptions = None,
**data,
):
...
def calculate(self, data: InputData) -> CustomCallableMetricResult:
...
@default_renderer(wrap_type=CustomValueMetric)
class CustomValueMetricRenderer(MetricRenderer):
def render_html(self, obj: CustomValueMetric) -> List[BaseWidgetInfo]:
...
Import
from evidently.legacy.metrics.custom_metric import CustomValueMetric
from evidently.legacy.metrics.custom_metric import CustomCallableMetricResult
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| func | Union[Callable[[InputData], float], str] |
Yes | A callable that takes InputData and returns a float, or a string with the function's qualified name.
|
| title | Optional[str] |
No | Display title for the metric widget. Defaults to an empty string. |
| size | Optional[WidgetSize] |
No | Widget size (WidgetSize.HALF or WidgetSize.FULL). Defaults to FULL.
|
| options | AnyOptions |
No | Metric options (color, render, etc.). |
Outputs
| Name | Type | Description |
|---|---|---|
| return | CustomCallableMetricResult |
A result object containing value: float computed by the provided callable.
|
Usage Examples
from evidently.legacy.metrics.custom_metric import CustomValueMetric
from evidently.legacy.report import Report
# Define a custom function that computes mean of the target column
def mean_target(data):
return data.current_data["target"].mean()
# Use CustomValueMetric in a report
report = Report(metrics=[
CustomValueMetric(func=mean_target, title="Mean Target Value")
])
report.run(reference_data=ref_df, current_data=curr_df)
# Use with half-width widget
from evidently.legacy.renderers.html_widgets import WidgetSize
report = Report(metrics=[
CustomValueMetric(func=mean_target, title="Mean Target", size=WidgetSize.HALF)
])
report.run(reference_data=ref_df, current_data=curr_df)