{"record":{"id":"8b853356c2d61773","repo":"mastra-ai/mastra","slug":"context-array-cannot-be-empty-if-provided-8b8533","errorCode":null,"errorMessage":"Context array cannot be empty if provided","messagePattern":"Context array cannot be empty if provided","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/evals/src/scorers/llm/context-recall/index.ts","lineNumber":65,"sourceCode":"  if (options.contextExtractor && isScorerRunInputForAgent(input) && isScorerRunOutputForAgent(output)) {\n    return options.contextExtractor(input, output);\n  }\n\n  return options.context ?? [];\n};\n\nexport function createContextRecallScorer({\n  model,\n  options,\n}: {\n  model: MastraModelConfig;\n  options: ContextRecallMetricOptions;\n}) {\n  if (!options.context && !options.contextExtractor) {\n    throw new Error('Either context or contextExtractor is required for Context Recall scoring');\n  }\n  if (options.context && options.context.length === 0) {\n    throw new Error('Context array cannot be empty if provided');\n  }\n\n  return createScorer<ScorerRunInputForLLMJudge, ScorerRunOutputForLLMJudge>({\n    id: 'context-recall-scorer',\n    name: 'Context Recall Scorer',\n    description:\n      'A scorer that evaluates how well retrieved context covers the claims in a ground-truth reference answer',\n    judge: {\n      model,\n      instructions: CONTEXT_RECALL_AGENT_INSTRUCTIONS,\n    },\n    type: 'agent',\n  })\n    .preprocess({\n      description: 'Extract atomic claims from the ground-truth answer',\n      outputSchema: claimExtractionOutputSchema,\n      createPrompt: ({ run }) => {\n        if (!run.groundTruth) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/evals/src/scorers/llm/context-recall/index.ts#L47-L83","documentation":"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'.","triggerScenarios":"Passing options.context: [] to createContextRecallScorer; passing the result of a filter/map over an empty retrieved-documents array as static context.","commonSituations":"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.","solutions":["Ensure at least one context entry exists before constructing the scorer; otherwise throw/log upstream","Switch to contextExtractor if context varies per run and should be validated per-run","Debug the retrieval pipeline (index contents, query, filters) that produced zero chunks","Add a call-site guard: if (!context.length) skip scorer creation"],"exampleFix":"// before\nconst scorer = createContextRecallScorer({ model, options: { context: docs.map(d => d.content) } }); // docs: []\n\n// after\nconst context = docs.map(d => d.content);\nif (!context.length) throw new Error('Retrieval returned no documents; check index/collection');\nconst scorer = createContextRecallScorer({ model, options: { context } });","handlingStrategy":"validation","validationCode":"const context = docs.map(d => d.content);\nif (options.context !== undefined && options.context.length === 0) {\n  throw new Error('Empty context passed to Context Recall; check retrieval');\n}\nconst scorer = createContextRecallScorer({ model, options: { ...options, context } });","typeGuard":null,"tryCatchPattern":"try {\n  const scorer = createContextRecallScorer({ model, options });\n} catch (err) {\n  if ((err as Error).message === 'Context array cannot be empty if provided') {\n    throw new DataError('Zero-context dataset row; inspect retrieval pipeline');\n  }\n  throw err;\n}","preventionTips":["Guard against empty arrays at the retrieval boundary before building options","Prefer contextExtractor (validated per-run) over a possibly-empty static array","Keep datasets with a non-empty context column; assert this in fixtures","Distinguish 'no context yet' (undefined) from 'retrieval failed' (empty array) in your pipeline"],"tags":["evals","scorer","context-recall","empty-array"],"backgroundTag":"empty-context-array","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}