Use context objects

Tasks, scorers, and metadata callbacks receive their inputs through context objects: typed Pydantic models that bundle all available data into a single parameter. This page is a reference for each context type.

TaskContext

TaskContext

A task function receives a TaskContext with the input record, system configuration, and run metadata:

from mistralai.evaluations import TaskContext

async def task(ctx: TaskContext) -> str:
    response = await client.chat.complete_async(
        model=str(ctx.system.params["model"]),
        messages=[{"role": "user", "content": ctx.input_record["prompt"]}],
    )
    return str(response.choices[0].message.content)
FieldTypeDescription
input_recorddict[str, Any]The current dataset record
systemSystem | NoneSystem config from evaluation.run(system=...)
metadatadict[str, Any] | NoneRun metadata from evaluation.run(metadata=...)
ScorerContext

ScorerContext

A scorer function receives a ScorerContext with the input record and the task output:

from mistralai.evaluations import ScorerContext

def accuracy_scorer(ctx: ScorerContext) -> int:
    return 1 if ctx.input_record["expected"].lower() in str(ctx.output).lower() else 0
FieldTypeDescription
input_recorddict[str, Any]The current dataset record
outputAnyThe task output for this generation
systemSystem | NoneSystem config from evaluation.run(system=...)
metadatadict[str, Any] | NoneRun metadata from evaluation.run(metadata=...)
RunEvaluatorContext

RunEvaluatorContext

Run evaluators receive all records and aggregated statistics after the run completes:

from mistralai.evaluations import RunEvaluatorContext

def accuracy_gate(ctx: RunEvaluatorContext):
    return ctx.statistics["accuracy"].avg >= 0.8
FieldTypeDescription
recordslist[RunEvaluatorRecord]All processed records with their scores
statisticsdict[str, EvaluatorStatistics]Per-evaluator aggregate statistics
metadatadict[str, JsonValue]Run metadata
systemSystem | NoneSystem config from evaluation.run(system=...)

Use get_score(record, "evaluator_name") to access a specific evaluator's score for a given record.

RecordMetadataContext

RecordMetadataContext

A record_metadata callback receives a RecordMetadataContext once a record's task and scorers have finished. Its returned dict is stored on the record's metadata in Studio:

from mistralai.evaluations import RecordMetadataContext

def record_metadata(ctx: RecordMetadataContext) -> dict:
    return {"output_len": len(str(ctx.record.generations[0].output))}
FieldTypeDescription
input_recorddict[str, Any]The current dataset record
recordEvaluationRunRecordThe processed record, including its generations and scores
systemSystem | NoneSystem config from evaluation.run(system=...)
metadatadict[str, JsonValue]Run metadata
RunMetadataContext

RunMetadataContext

A metadata callback (when passed a function instead of a dict) receives a RunMetadataContext after all records are scored. Its returned dict is merged into the run metadata:

from mistralai.evaluations import RunMetadataContext

def run_metadata(ctx: RunMetadataContext) -> dict:
    return {"num_records": len(ctx.records)}
FieldTypeDescription
recordslist[RunEvaluatorRecord]All processed records with their scores
statisticsdict[str, EvaluatorStatistics]Per-evaluator aggregate statistics
run_scoresdict[str, Score]Run-level evaluator scores
metadatadict[str, JsonValue]Run metadata
systemSystem | NoneSystem config from evaluation.run(system=...)