{"record":{"id":"b48fd4be96507c6e","repo":"mastra-ai/mastra","slug":"either-context-or-contextextractor-is-required-for-b48fd4","errorCode":null,"errorMessage":"Either context or contextExtractor is required for Context Relevance scoring","messagePattern":"Either context or contextExtractor is required for Context Relevance scoring","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/evals/src/scorers/llm/context-relevance/index.ts","lineNumber":74,"sourceCode":"  output: ScorerRunOutputForLLMJudge;\n  options: ContextRelevanceOptions;\n}) => {\n  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,","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/evals/src/scorers/llm/context-relevance/index.ts#L56-L92","documentation":"createContextRelevanceScorerLLM requires the content to judge: either a literal `context` array of retrieved chunks, or a `contextExtractor` function that pulls it from the run at scoring time. Without both, the scorer would have nothing to evaluate relevance against, so the factory throws synchronously at construction time rather than failing silently per-run.","triggerScenarios":"Calling createContextRelevanceScorerLLM({ model, options }) where options.context is undefined/null AND options.contextExtractor is undefined/null — e.g. constructing the scorer with only { model } or with options that omit both fields.","commonSituations":"Copy-pasting a scorer config from docs that used contextExtractor but forgetting to define it; wiring the scorer in a config object where context is expected to be injected later but never is; migrating from a scorer version where context was read from run input automatically.","solutions":["Pass a context array of retrieved chunks in options.context","Or pass options.contextExtractor: ({ input, output }) => string[] to extract context from the run","If context comes from a trace/span source, wire the extractor before constructing the scorer instead of leaving the field undefined"],"exampleFix":"// before\nconst scorer = createContextRelevanceScorerLLM({ model: 'openai/gpt-4o', options: {} });\n// after\nconst scorer = createContextRelevanceScorerLLM({\n  model: 'openai/gpt-4o',\n  options: {\n    contextExtractor: ({ input }) => input.context ?? [],\n  },\n});","handlingStrategy":"validation","validationCode":"function canCreateContextRelevance(options) {\n  return Boolean(options?.context?.length || typeof options?.contextExtractor === 'function');\n}\nif (!canCreateContextRelevance(opts)) throw new Error('Provide context or contextExtractor before creating the scorer');","typeGuard":"function hasContextSource(o) {\n  return (Array.isArray(o.context) && o.context.length > 0) || typeof o.contextExtractor === 'function';\n}","tryCatchPattern":"try {\n  const scorer = createContextRelevanceScorerLLM({ model, options });\n} catch (e) {\n  if (e.message.includes('context or contextExtractor')) {\n    throw new Error('Scorer misconfigured: supply options.context or options.contextExtractor', { cause: e });\n  }\n  throw e;\n}","preventionTips":["Type your scorer options strictly so omitting both context and contextExtractor fails typecheck","Build scorer configs from a single factory that enforces the context-source invariant","Add a unit test asserting construction throws without a context source"],"tags":["configuration","validation","evals","missing-parameter"],"backgroundTag":"missing-required-scorer-option","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}