mastra-ai/mastra · error · Error
Either context or contextExtractor is required for Context R
Error message
Either context or contextExtractor is required for Context Relevance scoring
What it means
createContextRelevanceScorerLLM requires the content to judge: either a literal `context` array of retrieved chunks, or a `contextExtractor` function that pulls it from the run at scoring time. Without both, the scorer would have nothing to evaluate relevance against, so the factory throws synchronously at construction time rather than failing silently per-run.
Source
Thrown at packages/evals/src/scorers/llm/context-relevance/index.ts:74
output: ScorerRunOutputForLLMJudge;
options: ContextRelevanceOptions;
}) => {
if (options.contextExtractor && isScorerRunInputForAgent(input) && isScorerRunOutputForAgent(output)) {
return options.contextExtractor(input, output);
}
return options.context ?? [];
};
export function createContextRelevanceScorerLLM({
model,
options,
}: {
model: MastraModelConfig;
options: ContextRelevanceOptions;
}) {
if (!options.context && !options.contextExtractor) {
throw new Error('Either context or contextExtractor is required for Context Relevance scoring');
}
if (options.context && options.context.length === 0) {
throw new Error('Context array cannot be empty if provided');
}
return createScorer<ScorerRunInputForLLMJudge, ScorerRunOutputForLLMJudge>({
id: 'context-relevance-scorer',
name: 'Context Relevance (LLM)',
description: 'Evaluates how relevant and useful the provided context was for generating the agent response',
judge: {
model,
instructions: CONTEXT_RELEVANCE_INSTRUCTIONS,
},
type: 'agent',
})
.analyze({
description: 'Analyze the relevance and utility of provided context',
outputSchema: analyzeOutputSchema,View on GitHub (pinned to 75dd419e61)
Solutions
- Pass a context array of retrieved chunks in options.context
- Or pass options.contextExtractor: ({ input, output }) => string[] to extract context from the run
- If context comes from a trace/span source, wire the extractor before constructing the scorer instead of leaving the field undefined
Example fix
// before
const scorer = createContextRelevanceScorerLLM({ model: 'openai/gpt-4o', options: {} });
// after
const scorer = createContextRelevanceScorerLLM({
model: 'openai/gpt-4o',
options: {
contextExtractor: ({ input }) => input.context ?? [],
},
}); Defensive patterns
Strategy: validation
Validate before calling
function canCreateContextRelevance(options) {
return Boolean(options?.context?.length || typeof options?.contextExtractor === 'function');
}
if (!canCreateContextRelevance(opts)) throw new Error('Provide context or contextExtractor before creating the scorer'); Type guard
function hasContextSource(o) {
return (Array.isArray(o.context) && o.context.length > 0) || typeof o.contextExtractor === 'function';
} Try / catch
try {
const scorer = createContextRelevanceScorerLLM({ model, options });
} catch (e) {
if (e.message.includes('context or contextExtractor')) {
throw new Error('Scorer misconfigured: supply options.context or options.contextExtractor', { cause: e });
}
throw e;
} Prevention
- Type your scorer options strictly so omitting both context and contextExtractor fails typecheck
- Build scorer configs from a single factory that enforces the context-source invariant
- Add a unit test asserting construction throws without a context source
When it happens
Trigger: Calling createContextRelevanceScorerLLM({ model, options }) where options.context is undefined/null AND options.contextExtractor is undefined/null — e.g. constructing the scorer with only { model } or with options that omit both fields.
Common situations: Copy-pasting a scorer config from docs that used contextExtractor but forgetting to define it; wiring the scorer in a config object where context is expected to be injected later but never is; migrating from a scorer version where context was read from run input automatically.
Related errors
- Both baselineResponse and noisyQuery are required for Noise
- NO_SCORERS_PROVIDED
- Context array cannot be empty if provided
- createMultiTurnJudgeScorer: options.scale must be a finite n
- Google RBAC roleMapping is required.
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/b48fd4be96507c6e.
Report an issue: GitHub.