createScorer
Mastra allows you to define your own custom code scorers for evaluating input/output pairs using any logic you choose. Custom scorers integrate seamlessly with the Mastra scoring framework and can be used anywhere built-in scorers are used.
For a usage example, see the Custom Code Scorer Examples.
How to Create a Custom Scorer
Use the createScorer
factory to define your scorer. You must provide at least a name
, description
, and an analyze
function. Optionally, you can provide extract
and reason
functions for multi-step or more advanced logic.
createScorer Options
name:
description:
analyze:
extract:
reason:
metadata:
This function returns an instance of the MastraScorer class. See the MastraScorer reference for details on the .run()
method and its input/output.
Step Function Signatures
extract
input:
output:
Returns: { results: any }
The method must return an object with a results
property. The value of results
will be passed to the analyze function as extractStepResult
.
analyze
input:
output:
extractStepResult:
Returns: { score: number, results?: any }
The method must return an object with a score
property (required). Optionally, it may return a results
property. The value of results
will be passed to the reason function as analyzeStepResult
.
reason
input:
output:
score:
analyzeStepResult:
extractStepResult:
Returns: { reason: string }
The method must return an object with a reason
property, which should be a string explaining the score.
All step functions can be async.