{"record":{"id":"e84bdd826d72d1e0","repo":"mastra-ai/mastra","slug":"context-array-cannot-be-empty-if-provided-e84bdd","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-relevance/index.ts","lineNumber":77,"sourceCode":"  if (options.contextExtractor && isScorerRunInputForAgent(input) && isScorerRunOutputForAgent(output)) {\n    return options.contextExtractor(input, output);\n  }\n\n  return options.context ?? [];\n};\n\nexport function createContextRelevanceScorerLLM({\n  model,\n  options,\n}: {\n  model: MastraModelConfig;\n  options: ContextRelevanceOptions;\n}) {\n  if (!options.context && !options.contextExtractor) {\n    throw new Error('Either context or contextExtractor is required for Context Relevance 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-relevance-scorer',\n    name: 'Context Relevance (LLM)',\n    description: 'Evaluates how relevant and useful the provided context was for generating the agent response',\n    judge: {\n      model,\n      instructions: CONTEXT_RELEVANCE_INSTRUCTIONS,\n    },\n    type: 'agent',\n  })\n    .analyze({\n      description: 'Analyze the relevance and utility of provided context',\n      outputSchema: analyzeOutputSchema,\n      createPrompt: ({ run }) => {\n        const userQuery = getUserMessageFromRunInput(run.input) ?? '';\n        const agentResponse = getAssistantMessageFromRunOutput(run.output) ?? '';","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/evals/src/scorers/llm/context-relevance/index.ts#L59-L95","documentation":"If `options.context` is provided to createContextRelevanceScorerLLM it must contain at least one entry; an empty array is rejected because there is no content to score relevance against. The factory treats an explicitly empty context as a configuration mistake rather than a valid 'no context' case.","triggerScenarios":"Calling createContextRelevanceScorerLLM({ model, options: { context: [] } }) — a context key that exists but has zero elements, typically from an upstream retrieval step that returned no documents.","commonSituations":"A RAG retriever returned an empty result set and its output was passed straight through as context; a filter on retrieved chunks removed everything; initializing context as [] before an async fetch and constructing the scorer before the fetch resolves.","solutions":["Guard the retrieval step: only construct the scorer when retrievedChunks.length > 0","If context may be empty, omit the context field and supply a contextExtractor that resolves at run time","Surface an upstream warning when retrieval returns zero chunks so empty arrays are not silently propagated"],"exampleFix":"// before\nconst scorer = createContextRelevanceScorerLLM({ model, options: { context: retrieved ?? [] } });\n// after\nconst options = retrieved && retrieved.length > 0 ? { context: retrieved } : { contextExtractor: ({ input }) => input.context ?? [] };\nconst scorer = createContextRelevanceScorerLLM({ model, options });","handlingStrategy":"validation","validationCode":"if (opts.context && (!Array.isArray(opts.context) || opts.context.length === 0)) {\n  throw new Error('context, when provided, must be a non-empty array');\n}","typeGuard":"function isNonEmptyContext(o) {\n  return !('context' in o) || (Array.isArray(o.context) && o.context.length > 0);\n}","tryCatchPattern":"try {\n  const scorer = createContextRelevanceScorerLLM({ model, options });\n} catch (e) {\n  if (e.message.includes('Context array cannot be empty')) {\n    console.warn('Retrieval returned zero chunks; falling back to runtime extractor');\n  }\n  throw e;\n}","preventionTips":["Never default context to []; leave it undefined and use a contextExtractor instead","Log retrieval result counts so empty retrievals are visible before scoring","Guard the retrieval→scorer pipeline with a chunks.length check"],"tags":["configuration","validation","evals","empty-array"],"backgroundTag":"empty-input-validation","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}