{"record":{"id":"11174a47e94b3763","repo":"mastra-ai/mastra","slug":"context-array-cannot-be-empty-if-provided","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-precision/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 createContextPrecisionScorer({\n  model,\n  options,\n}: {\n  model: MastraModelConfig;\n  options: ContextPrecisionMetricOptions;\n}) {\n  if (!options.context && !options.contextExtractor) {\n    throw new Error('Either context or contextExtractor is required for Context Precision 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-precision-scorer',\n    name: 'Context Precision Scorer',\n    description:\n      'A scorer that evaluates the relevance and precision of retrieved context nodes for generating expected outputs',\n    judge: {\n      model,\n      instructions: CONTEXT_PRECISION_AGENT_INSTRUCTIONS,\n    },\n    type: 'agent',\n  })\n    .analyze({\n      description: 'Evaluate the relevance of each context piece for generating the expected output',\n      outputSchema: contextRelevanceOutputSchema,\n      createPrompt: ({ run }) => {\n        const input = getUserMessageFromRunInput(run.input) ?? '';","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/evals/src/scorers/llm/context-precision/index.ts#L47-L83","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Populate options.context with at least one retrieved chunk before creating the scorer","If context is computed per-run, use contextExtractor instead of an empty static array","Check the upstream retrieval step for why it produced zero chunks (index empty, embedding failure, filters too strict)","Guard the call site: only create the scorer when context.length > 0"],"exampleFix":"// before\nconst scorer = createContextPrecisionScorer({ model, options: { context: retrieved ?? [] } });\n\n// after\nif (!retrieved?.length) throw new Error('No retrieved context for this dataset');\nconst scorer = createContextPrecisionScorer({ model, options: { context: retrieved } });","handlingStrategy":"validation","validationCode":"if (options.context && options.context.length === 0) {\n  throw new Error('Provided context is empty; check retrieval pipeline before scoring');\n}\nconst scorer = createContextPrecisionScorer({ model, options });","typeGuard":null,"tryCatchPattern":"try {\n  const scorer = createContextPrecisionScorer({ model, options });\n} catch (err) {\n  if ((err as Error).message === 'Context array cannot be empty if provided') {\n    throw new DataError('Retrieval produced zero chunks; inspect the retrieval step');\n  }\n  throw err;\n}","preventionTips":["Never pass context: [] — pass undefined and use contextExtractor instead","Assert non-empty context at the retrieval boundary before building scorer options","Initialize context lazily (undefined) so empty states are distinguishable from unpopulated ones","Monitor retrieval hit rates in RAG eval datasets"],"tags":["evals","scorer","context-precision","empty-array"],"backgroundTag":"empty-context-array","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}