mastra-ai/mastra · error · Error

Context array cannot be empty if provided

Error message

Context array cannot be empty if provided

What it means

This companion check to the context/contextExtractor requirement rejects an explicitly provided but empty context array. An empty array signals the caller intended to supply context but produced none — likely a data-extraction bug — so the factory throws instead of silently creating a scorer that always fails at runtime with 'No context available'. Located at packages/evals/src/scorers/llm/context-precision/index.ts:65.

Source

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

  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',
      outputSchema: contextRelevanceOutputSchema,
      createPrompt: ({ run }) => {
        const input = getUserMessageFromRunInput(run.input) ?? '';

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Populate options.context with at least one retrieved chunk before creating the scorer
  2. If context is computed per-run, use contextExtractor instead of an empty static array
  3. Check the upstream retrieval step for why it produced zero chunks (index empty, embedding failure, filters too strict)
  4. Guard the call site: only create the scorer when context.length > 0

Example fix

// before
const scorer = createContextPrecisionScorer({ model, options: { context: retrieved ?? [] } });

// after
if (!retrieved?.length) throw new Error('No retrieved context for this dataset');
const scorer = createContextPrecisionScorer({ model, options: { context: retrieved } });
Defensive patterns

Strategy: validation

Validate before calling

if (options.context && options.context.length === 0) {
  throw new Error('Provided context is empty; check retrieval pipeline before scoring');
}
const scorer = createContextPrecisionScorer({ model, options });

Try / catch

try {
  const scorer = createContextPrecisionScorer({ model, options });
} catch (err) {
  if ((err as Error).message === 'Context array cannot be empty if provided') {
    throw new DataError('Retrieval produced zero chunks; inspect the retrieval step');
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing options.context = [] to createContextPrecisionScorer; a pipeline that builds the context array by filtering retrieved documents where nothing matched, then passes the empty result statically.

Common situations: RAG datasets where the retrieval step returned zero chunks; mapping over an empty fixtures column; initializing context as [] and never populating it due to an async bug.

Related errors


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