mastra-ai/mastra · error · Error

Context array cannot be empty if provided

Error message

Context array cannot be empty if provided

What it means

Companion check in createContextRecallScorer rejecting an explicitly provided but empty context array (packages/evals/src/scorers/llm/context-recall/index.ts:65). An empty array means the caller intended to supply context but extracted none — typically an upstream retrieval or data-pipeline bug — so the factory fails fast rather than creating a scorer that throws at runtime with 'No context available'.

Source

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

  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',
      outputSchema: claimExtractionOutputSchema,
      createPrompt: ({ run }) => {
        if (!run.groundTruth) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure at least one context entry exists before constructing the scorer; otherwise throw/log upstream
  2. Switch to contextExtractor if context varies per run and should be validated per-run
  3. Debug the retrieval pipeline (index contents, query, filters) that produced zero chunks
  4. Add a call-site guard: if (!context.length) skip scorer creation

Example fix

// before
const scorer = createContextRecallScorer({ model, options: { context: docs.map(d => d.content) } }); // docs: []

// after
const context = docs.map(d => d.content);
if (!context.length) throw new Error('Retrieval returned no documents; check index/collection');
const scorer = createContextRecallScorer({ model, options: { context } });
Defensive patterns

Strategy: validation

Validate before calling

const context = docs.map(d => d.content);
if (options.context !== undefined && options.context.length === 0) {
  throw new Error('Empty context passed to Context Recall; check retrieval');
}
const scorer = createContextRecallScorer({ model, options: { ...options, context } });

Try / catch

try {
  const scorer = createContextRecallScorer({ model, options });
} catch (err) {
  if ((err as Error).message === 'Context array cannot be empty if provided') {
    throw new DataError('Zero-context dataset row; inspect retrieval pipeline');
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing options.context: [] to createContextRecallScorer; passing the result of a filter/map over an empty retrieved-documents array as static context.

Common situations: Retrieval returned zero chunks for the whole dataset (empty index, wrong collection name); fixtures with an empty context column; variable initialized as [] and never assigned due to an unawaited promise.

Related errors


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