mistralM

Docs

Use context objects

Reference for the typed context objects passed to tasks, scorers, run evaluators, and metadata callbacks.

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.

The SDK passes a context object only when the function's single parameter is annotated with the context type (ctx: TaskContext, ctx: ScorerContext). Without the annotation, the SDK calls the function with the raw record.

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)

Field

Type

Description

input_recorddict[str, Any]The current dataset record
systemSystem | NoneSystem config from evaluation.run(system=...)
metadatadict[str, Any]Run metadata (empty dict if none) from evaluation.run(metadata=...)

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

Field

Type

Description

input_recorddict[str, Any]The current dataset record
outputAnyThe task output for this generation
systemSystem | NoneSystem config from evaluation.run(system=...)
metadatadict[str, Any]Run metadata (empty dict if none) from evaluation.run(metadata=...)
record_metadatadict[str, JsonValue]Persisted record metadata. Empty during run(), filled by rescore()
generation_metadatadict[str, JsonValue]Persisted generation metadata. Empty during run(), filled by rescore()

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

Field

Type

Description

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

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))}

Field

Type

Description

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

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)}

Field

Type

Description

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=...)