mastra-ai/mastra · error · Error
Either context or contextExtractor is required for Context P
Error message
Either context or contextExtractor is required for Context Precision scoring
What it means
createContextPrecisionScorer needs retrieved context to judge: it must come either from a static options.context array or be computed per-run via options.contextExtractor. If neither is supplied the scorer cannot obtain context at scoring time, so the factory throws immediately. This is construction-time configuration validation in packages/evals/src/scorers/llm/context-precision/index.ts:62.
Source
Thrown at packages/evals/src/scorers/llm/context-precision/index.ts:62
output: ScorerRunOutputForLLMJudge;
options: ContextPrecisionMetricOptions;
}) => {
if (options.contextExtractor && isScorerRunInputForAgent(input) && isScorerRunOutputForAgent(output)) {
return options.contextExtractor(input, output);
}
return options.context ?? [];
};
export function createContextPrecisionScorer({
model,
options,
}: {
model: MastraModelConfig;
options: ContextPrecisionMetricOptions;
}) {
if (!options.context && !options.contextExtractor) {
throw new Error('Either context or contextExtractor is required for Context Precision scoring');
}
if (options.context && options.context.length === 0) {
throw new Error('Context array cannot be empty if provided');
}
return createScorer<ScorerRunInputForLLMJudge, ScorerRunOutputForLLMJudge>({
id: 'context-precision-scorer',
name: 'Context Precision Scorer',
description:
'A scorer that evaluates the relevance and precision of retrieved context nodes for generating expected outputs',
judge: {
model,
instructions: CONTEXT_PRECISION_AGENT_INSTRUCTIONS,
},
type: 'agent',
})
.analyze({
description: 'Evaluate the relevance of each context piece for generating the expected output',View on GitHub (pinned to 75dd419e61)
Solutions
- Pass context: ['retrieved chunk 1', ...] for a fixed context set
- Pass contextExtractor: ({ run }) => string[] to pull context from each run (e.g. from retrievedDocuments)
- Verify both keys are spelled exactly context and contextExtractor on the options object
- If you actually need recall-style checking, confirm you are using the right metric, but note it has the same requirement
Example fix
// before
const scorer = createContextPrecisionScorer({ model: 'openai/gpt-4o', options: {} });
// after
const scorer = createContextPrecisionScorer({
model: 'openai/gpt-4o',
options: { contextExtractor: ({ run }) => run.input?.retrievedDocuments?.map(d => d.content) ?? [] },
}); Defensive patterns
Strategy: validation
Validate before calling
function validateContextOptions(o: ContextPrecisionMetricOptions): void {
if (!o.context && !o.contextExtractor) {
throw new Error('Context Precision needs options.context or options.contextExtractor');
}
}
validateContextOptions(options); Type guard
function hasContextSource(o: ContextPrecisionMetricOptions): o is ContextPrecisionMetricOptions & ({ context: string[] } | { contextExtractor: (...a: unknown[]) => string[] }) {
return Boolean(o.context) || Boolean(o.contextExtractor);
} Try / catch
try {
const scorer = createContextPrecisionScorer({ model, options });
} catch (err) {
if ((err as Error).message.includes('context or contextExtractor is required')) {
throw new ConfigError('Wire context or contextExtractor into Context Precision options');
}
throw err;
} Prevention
- Always wire either context or contextExtractor when creating context-based scorers
- Use contextExtractor to pull from run.input.retrievedDocuments for RAG evals
- Validate options with a schema at config load time
- Spell keys exactly: context, contextExtractor
When it happens
Trigger: Calling createContextPrecisionScorer({ model, options: {} }); passing options without context and without contextExtractor (e.g. only scale or other metric options); building options from config where both keys were dropped.
Common situations: Copying a context-precision example and deleting the context option; expecting the scorer to auto-derive context from the run's retrievedDocuments (it does not unless contextExtractor reads them); switching scorers and not updating the options object.
Related errors
- MASTR_SCORER_FAILED_TO_CREATE_MISSING_ID
- Either expectedTool or expectedToolOrder must be provided
- Context array cannot be empty if provided
- No context available for evaluation
- Either context or contextExtractor is required for Context R
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/5b3600569f270ecb.
Report an issue: GitHub.