- Studio
- Observability
- Offline evaluations
Configure statistics
Configure statistics
Statistics are the run-level values an evaluator exposes from its per-record scores: an average, a total, a percentile, or a classification metric such as F1. They are what you see in the run table header, chart over time, and target with a goal.
By default an evaluator computes the historical set (avg, min, max, std, count). Declare statistics when you want a different set: a total number of failures, latency percentiles, or nothing at all.
Each declared statistic is a serializable declaration understood by the SDK, the backend, and Studio alike, so it can be recomputed after filtering or rescoring, charted, compared, and gated by a goal.
Quick example
from mistralai.evaluations import Evaluator, Goal, Statistic
Evaluator(
name="latency_ms",
description="End-to-end response time in milliseconds.",
scorer=latency_scorer,
statistics=[
Statistic.percentile(95, goal=Goal.lte(500)),
Statistic.percentile(50),
Statistic.percentile(75),
],
)
The factories
The Statistic factory declares each built-in reduction:
Method | Meaning | Id |
|---|---|---|
| Statistic.avg() | Arithmetic mean of the numeric scores | avg |
| Statistic.avg(weight_key="cells") | Weighted mean, each score weighted by metadata["cells"] (see Weighted mean) | avg_by(cells) |
| Statistic.sum() | Sum of the numeric scores (for example, a total number of failures) | sum |
| Statistic.min() | Smallest numeric score | min |
| Statistic.max() | Largest numeric score | max |
| Statistic.std() | Population standard deviation | std |
| Statistic.count() | Number of successful, non-null numeric scores | count |
| Statistic.percentile(p) | The p-th percentile, 0 <= p <= 100 | p50, p95, p99.9, … |
Each id is canonical and stable: avg, sum, min, max, std, count, p{p} for percentiles (p50, p95), and avg_by({weight_key}) for a weighted mean. Ids are how a statistic is referenced across the SDK, the run payload, and the UI, so they never change once declared. A median is Statistic.percentile(50): there is no separate factory for it.
There is deliberately no rate reduction: a rate is avg over 0/1 scores.
Omission vs. an explicit list
The statistics field has two modes:
- Omitted (the default) — historical behavior is preserved. Studio detects the score type and, for numeric scores, exposes the default set avg, min, max, std, count.
- An explicit list — authoritative. Exactly these statistics are computed and exposed, in the order given, with no implicit defaults added. An empty list statistics=[] means the evaluator exposes no run-level statistics.
The first entry of the effective list is the evaluator's headline statistic: the single value shown in the run table header for compact display. It is a presentation convention, not a persisted flag.
Examples
Default set (omission)
Omitting statistics keeps the default set:
Evaluator(name="quality", scorer=quality_scorer)
# exposes avg, min, max, std, count — avg is the headline
Total of a 0/1 check
An evaluator whose scorer returns 0/1 per record (for example, 1 when the answer breaks a rule), exposing only the total, gated at zero:
from mistralai.evaluations import Evaluator, Goal, Statistic
Evaluator(
name="rule_breaks",
description="1 when the answer breaks the rule, 0 otherwise.",
scorer=rule_break_scorer, # returns numeric 0 or 1
statistics=[Statistic.sum(goal=Goal.lte(0))],
)
sum totals the 0/1 scores, and its goal fails if any answer broke the rule.
This doesn't count records whose task failed: evaluators don't run on a failed generation, so it is missing from every statistic. Read run.failed_record_count (out of len(run.output_records)) to see them, and run them again with retry_failed_records().
Latency percentiles
from mistralai.evaluations import Evaluator, Goal, Statistic
Evaluator(
name="latency_ms",
description="End-to-end response time in milliseconds.",
scorer=latency_scorer,
statistics=[
Statistic.percentile(95, goal=Goal.lte(500)),
Statistic.percentile(50),
Statistic.percentile(75),
],
)
This exposes exactly p50, p75, and p95 — no avg, min, max, std, or count. Because it is first, p95 is the headline statistic shown in the run table header, and its Goal.lte(500) fails the run when the 95th percentile latency exceeds 500 ms.
Weighted mean
By default, every record weighs 1 in a statistic. When records differ in size, the plain mean of per-record scores can tell a different story than the whole run.
Take a document extraction task scored per document as correct fields / total fields. A 4-field invoice extracted perfectly and a 40-field statement with 14 correct fields average to 67.5%, but only 18 of 44 fields (41%) are correct.
Put each record's weight in the score metadata and declare Statistic.avg(weight_key=...). The value is sum(weight * score) / sum(weight), here sum(correct) / sum(total):
from mistralai.evaluations import Evaluator, Goal, Score, ScorerContext, Statistic
def field_accuracy(ctx: ScorerContext) -> Score:
correct, total = compare_fields(ctx.output, ctx.input_record["expected_fields"])
return Score(value=correct / total, metadata={"fields": total})
Evaluator(
name="field_accuracy",
scorer=field_accuracy,
statistics=[
Statistic.avg(weight_key="fields", goal=Goal.gte(0.7)), # headline: share of correct fields
Statistic.avg(), # mean of per-document accuracies
],
)
Each row still shows its own per-document score. The weighted mean is a run-level statistic like the others, so it takes a goal and is recomputed when you filter records in Studio. Its id keeps the key verbatim (avg_by(fields)), so you can declare a plain and a weighted avg together.
A score whose weight is missing, not a number, or negative is left out of the weighted mean, and Studio shows how many were excluded. When the weights sum to 0, the value is null.
Any ratio of totals fits this shape: a word error rate is errors / words weighted by words, and a micro-averaged precision over extracted entities is tp / (tp + fp) weighted by tp + fp.
The weighted mean requires SDK 0.11.0+. Before, a run-level figure such as "correct fields / all fields" needed a run evaluator.
Classification statistics
precision, recall, and f1 come from a confusion matrix over (expected, predicted) label pairs, so they need two labels per record. The predicted label is the score value. The expected label is read from the score metadata, under expected_key (default "expected").
Method | Meaning | Id |
|---|---|---|
| Statistic.precision(...) | Precision (positive predictive value) | precision, precision_macro, … |
| Statistic.recall(...) | Recall (true positive rate) | recall, recall_micro, … |
| Statistic.f1(...) | F1, the harmonic mean of precision and recall | f1, f1_weighted, … |
Each factory takes exactly one of:
- positive_label: a binary metric, computed against that one class.
- average: a multi-class metric, averaged with "macro", "micro", or "weighted".
Both modes also accept optional expected_key, labels, and goal arguments.
Binary
from mistralai.evaluations import Evaluator, Score, ScorerContext, Statistic
def scorer(ctx: ScorerContext) -> Score:
# value: the model's prediction; metadata["expected"]: the ground-truth label
return Score(value=ctx.output, metadata={"expected": ctx.input_record["expected"]})
Evaluator(
name="spam_classifier",
scorer=scorer,
statistics=[
Statistic.f1(positive_label="spam"), # headline
Statistic.precision(positive_label="spam"),
Statistic.recall(positive_label="spam"),
],
)
The labels are categorical scores, so the evaluator also gets the prediction frequencies. Studio shows the metrics next to the confusion matrix they were computed from, and sums the matrices across runs to aggregate the metrics in the run list.
Multi-class
statistics=[
Statistic.f1(average="macro"), # id: f1_macro
Statistic.precision(average="weighted"), # id: precision_weighted
Statistic.recall(average="micro"), # id: recall_micro
]
macro and weighted averages depend on which classes are present. Pass labels to pin the set of classes, for example Statistic.f1(average="macro", labels=["fr", "en", "de"]), so the value stays comparable when a filter removes every record of a class.
Undefined metrics and missing labels
- A metric is null (shown as -) when its denominator is zero, for example precision when the model never predicts the positive class.
- A score without an expected label in its metadata can't enter the confusion matrix. It's excluded from the metrics and counted in excluded_count, shown in Studio next to the number of scored records.
For accuracy, you don't need a classification statistic: score 1 when the prediction matches the expected label and 0 otherwise, then declare Statistic.avg().
Statistic-level goals
A goal attached to a statistic targets that statistic — at both the record and the run aggregation scope. This is the recommended way to gate a run and the replacement for aggregate_goal:
# Recommended
Evaluator(
name="accuracy",
scorer=accuracy_scorer,
statistics=[Statistic.avg(goal=Goal.gte(0.9))],
)
# Deprecated — prefer a statistic-level goal
Evaluator(
name="accuracy",
scorer=accuracy_scorer,
aggregate_goal=Goal.gte(0.9),
)
A statistic-level goal never carries its own metric: the target is the statistic it is attached to. (Only the deprecated aggregate_goal path uses metric to name a statistic.)
Scores must be numeric 0/1, not booleans
The numeric reductions need numeric scores. An evaluator that gates on pass/fail must emit numeric 0 or 1, not True/False. Booleans are classified as categorical values, so a boolean-returning scorer gets no numeric reductions: with statistics omitted it produces categorical statistics (frequencies and mode), and with a declared numeric statistic such as Statistic.sum() the statistic is unsupported and its goal is never evaluated.
from mistralai.evaluations import ScorerContext
def failure_scorer(ctx: ScorerContext) -> int:
return 1 if failed(ctx) else 0 # numeric — correct
def failure_scorer(ctx: ScorerContext) -> bool:
return failed(ctx) # boolean: no sum, the goal is never evaluated
The percentile algorithm
Statistic.percentile(p) uses the Hyndman-Fan type-7 algorithm (NumPy's default). For sorted scores x of length n and rank h = (n - 1) * p / 100, the result linearly interpolates between x[floor(h)] and x[ceil(h)]; a single score returns that score. The identical algorithm runs in the Python SDK, the TypeScript SDK, and the backend, so the same scores always produce the same value.
Empty samples
When no successful numeric scores fed the computation, a declared count is 0 and every other declared statistic is null (unevaluable). An empty dataset therefore never accidentally satisfies a goal like Statistic.sum(goal=Goal.lte(0)).