Implementation:Evidentlyai Evidently Metric Container
| Knowledge Sources | |
|---|---|
| Domains | Metrics, Presets, Data Monitoring |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Abstract base classes for metric containers that programmatically generate multiple related metrics, enabling presets and column-level metric generators.
Description
This module defines the core abstractions for grouping and generating metrics in Evidently:
MetricContainer is an abstract base class (extending EvidentlyBaseModel and AutoAliasMixin) that serves as the foundation for creating collections of metrics. Concrete subclasses implement generate_metrics(context) to produce a sequence of Metric or nested MetricContainer objects. Key features include:
- metrics(context) -- Returns generated metrics with caching via the context's fingerprint system
- render(context, child_widgets) -- Combines visualization widgets from child metrics into a flat list
- list_metrics(context) -- Recursively yields all leaf Metric objects, flattening nested containers
- _get_tests(tests) -- Handles the include_tests flag, converting test specifications or returning defaults
- include_tests -- Boolean flag controlling whether generated metrics include default tests
ColumnMetricContainer is a specialized subclass that adds a column attribute, simplifying containers that generate metrics for a single named column.
MetricOrContainer is a type alias (Union[Metric, MetricContainer]) used throughout the codebase to represent items that are either individual metrics or containers that expand into multiple metrics.
Usage
Subclass MetricContainer to create presets (e.g., DataDriftPreset) or generators (e.g., ColumnMetricGenerator) that produce multiple metrics based on dataset context. Subclass ColumnMetricContainer when the container logic is scoped to a single column.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File: src/evidently/core/container.py
Signature
MetricOrContainer = Union[Metric, "MetricContainer"]
class MetricContainer(AutoAliasMixin, EvidentlyBaseModel, abc.ABC):
include_tests: bool = True
def __init__(self, include_tests: bool = True, **data):
@abc.abstractmethod
def generate_metrics(self, context: "Context") -> Sequence[MetricOrContainer]:
def metrics(self, context: "Context") -> List[MetricOrContainer]:
def render(
self,
context: "Context",
child_widgets: Optional[List[Tuple[Optional[MetricId], List[BaseWidgetInfo]]]] = None,
) -> List[BaseWidgetInfo]:
def list_metrics(self, context: "Context") -> Generator[Metric, None, None]:
def _get_tests(self, tests):
class ColumnMetricContainer(MetricContainer, abc.ABC):
column: str
def __init__(self, column: str, include_tests: bool = True):
Import
from evidently.core.container import MetricContainer, ColumnMetricContainer, MetricOrContainer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| include_tests | bool | No (default: True) | Whether generated metrics should include default tests |
| context | Context | Yes (method arg) | Report context containing datasets and configuration, used for metric generation and caching |
| column | str | Yes (ColumnMetricContainer) | Name of the target column for column-scoped containers |
Outputs
| Name | Type | Description |
|---|---|---|
| generate_metrics return | Sequence[MetricOrContainer] | Sequence of Metric or MetricContainer objects produced by the container |
| metrics return | List[MetricOrContainer] | Cached list of generated metrics |
| list_metrics return | Generator[Metric] | Iterator over all leaf Metric objects (flattened) |
| render return | List[BaseWidgetInfo] | Combined visualization widgets from child metrics |
Usage Examples
from evidently.core.container import MetricContainer, MetricOrContainer
from evidently.core.report import Context
from typing import Sequence
class MyPreset(MetricContainer):
def generate_metrics(self, context: Context) -> Sequence[MetricOrContainer]:
# Return a list of metrics for this preset
return [
SomeMetric(column="feature_1"),
SomeMetric(column="feature_2"),
]
# Use in a Report
from evidently import Report
report = Report(metrics=[MyPreset()])
Related Pages
- Environment:Evidentlyai_Evidently_Python_Core_Environment
- Implementation:Evidentlyai_Evidently_Core_Tests - Test types used with the include_tests flag
- Implementation:Evidentlyai_Evidently_CLI_Report - CLI command that uses MetricOrContainer in report configuration