MastraScorer
The MastraScorer class is the base class for all scorers in Mastra. It provides a standard .run() method for evaluating input/output pairs and supports multi-step scoring workflows with preprocess → analyze → generateScore → generateReason execution flow.
Note: Most users should use createScorer to create scorer instances. Direct instantiation of MastraScorer isn't recommended.
How to get a MastraScorer instanceDirect link to how-to-get-a-mastrascorer-instance
Use the createScorer factory function, which returns a MastraScorer instance:
import { createScorer } from '@mastra/core/evals'
const scorer = createScorer({
name: 'My Custom Scorer',
description: 'Evaluates responses based on custom criteria',
}).generateScore(({ run, results }) => {
// scoring logic
return 0.85
})
// scorer is now a MastraScorer instance
.run() methodDirect link to run-method
The .run() method is the primary way to execute your scorer and evaluate input/output pairs. It processes the data through your defined steps (preprocess → analyze → generateScore → generateReason) and returns a comprehensive result object with the score, reasoning, and intermediate results.
const result = await scorer.run({
input: 'What is machine learning?',
output: 'Machine learning is a subset of artificial intelligence...',
runId: 'optional-run-id',
requestContext: {
/* optional context */
},
})
.run() inputDirect link to run-input
input:
output:
runId:
requestContext:
groundTruth:
.run() returnsDirect link to run-returns
runId:
score:
reason:
preprocessStepResult:
analyzeStepResult:
preprocessPrompt:
analyzePrompt:
generateScorePrompt:
generateReasonPrompt:
Step execution flowDirect link to Step execution flow
When you call .run(), the MastraScorer executes the defined steps in this order:
- preprocess (optional) - Extracts or transforms data
- analyze (optional) - Processes the input/output and preprocessed data
- generateScore (required) - Computes the numerical score
- generateReason (optional) - Provides explanation for the score
Each step receives the results from previous steps, allowing you to build complex evaluation pipelines.
Usage exampleDirect link to Usage example
const scorer = createScorer({
name: 'Quality Scorer',
description: 'Evaluates response quality',
})
.preprocess(({ run }) => {
// Extract key information
return { wordCount: run.output.split(' ').length }
})
.analyze(({ run, results }) => {
// Analyze the response
const hasSubstance = results.preprocessStepResult.wordCount > 10
return { hasSubstance }
})
.generateScore(({ results }) => {
// Calculate score
return results.analyzeStepResult.hasSubstance ? 1.0 : 0.0
})
.generateReason(({ score, results }) => {
// Explain the score
const wordCount = results.preprocessStepResult.wordCount
return `Score: ${score}. Response has ${wordCount} words.`
})
// Use the scorer
const result = await scorer.run({
input: 'What is machine learning?',
output: 'Machine learning is a subset of artificial intelligence...',
})
console.log(result.score) // 1.0
console.log(result.reason) // "Score: 1.0. Response has 12 words."
IntegrationDirect link to Integration
MastraScorer instances can be used for agents and workflow steps
See the createScorer reference for detailed information on defining custom scoring logic.