mastra-ai/mastra · error · Error

Either context or contextExtractor is required for Context R

Error message

Either context or contextExtractor is required for Context Recall scoring

What it means

createContextRecallScorer mirrors context precision: it requires context to judge how much of it the output covers, supplied via a static options.context array or a per-run options.contextExtractor. With neither, the scorer has no context to attribute statements against, so the factory throws at construction time (packages/evals/src/scorers/llm/context-recall/index.ts:62).

Source

Thrown at packages/evals/src/scorers/llm/context-recall/index.ts:62

  output: ScorerRunOutputForLLMJudge;
  options: ContextRecallMetricOptions;
}) => {
  if (options.contextExtractor && isScorerRunInputForAgent(input) && isScorerRunOutputForAgent(output)) {
    return options.contextExtractor(input, output);
  }

  return options.context ?? [];
};

export function createContextRecallScorer({
  model,
  options,
}: {
  model: MastraModelConfig;
  options: ContextRecallMetricOptions;
}) {
  if (!options.context && !options.contextExtractor) {
    throw new Error('Either context or contextExtractor is required for Context Recall scoring');
  }
  if (options.context && options.context.length === 0) {
    throw new Error('Context array cannot be empty if provided');
  }

  return createScorer<ScorerRunInputForLLMJudge, ScorerRunOutputForLLMJudge>({
    id: 'context-recall-scorer',
    name: 'Context Recall Scorer',
    description:
      'A scorer that evaluates how well retrieved context covers the claims in a ground-truth reference answer',
    judge: {
      model,
      instructions: CONTEXT_RECALL_AGENT_INSTRUCTIONS,
    },
    type: 'agent',
  })
    .preprocess({
      description: 'Extract atomic claims from the ground-truth answer',

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass the retrieved documents: options.context: ['chunk 1', 'chunk 2', ...]
  2. Or pass options.contextExtractor: ({ run }) => string[] to derive context per run
  3. Double-check the options key spelling (context / contextExtractor) and that the object is passed under options
  4. Confirm this is the metric you want; if scoring output quality without context, use a different scorer

Example fix

// before
const scorer = createContextRecallScorer({ model, options: { scale: 1 } });

// after
const scorer = createContextRecallScorer({
  model,
  options: { scale: 1, context: groundTruthContext },
});
Defensive patterns

Strategy: validation

Validate before calling

function validateContextRecallOptions(o: ContextRecallMetricOptions): void {
  if (!o.context && !o.contextExtractor) {
    throw new Error('Context Recall needs options.context or options.contextExtractor');
  }
}
validateContextRecallOptions(options);

Type guard

function hasRecallContextSource(o: ContextRecallMetricOptions): o is ContextRecallMetricOptions & ({ context: string[] } | { contextExtractor: (...a: unknown[]) => string[] }) {
  return Boolean(o.context) || Boolean(o.contextExtractor);
}

Try / catch

try {
  const scorer = createContextRecallScorer({ model, options });
} catch (err) {
  if ((err as Error).message.includes('context or contextExtractor is required')) {
    throw new ConfigError('Wire context or contextExtractor into Context Recall options');
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling createContextRecallScorer({ model, options: {} }) or with options lacking both context and contextExtractor; options objects reused from metrics that do not require context (e.g. answer-relevance options).

Common situations: Swapping an answer-relevance scorer for context recall and reusing the old options; config-driven scorer setup where the context key was never wired; examples copied without the context field.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/d19f156b710b817d. Report an issue: GitHub.