Use context objects

Tasks and scorers receive their inputs through context objects: typed Pydantic models that bundle all available data into a single parameter. This page is a reference for the three context types.

TaskContext

TaskContext

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

from mistralai.observability 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.observability 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.observability 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.