Skip to main content

getScorerByName()

The getScorerByName() method retrieves a scorer by searching for its name property rather than the registration key. This is useful when you know the scorer's display name but not necessarily how it was registered in the Mastra instance.

Usage Example

import { mastra } from "./mastra";

// Get a scorer by its name property
const relevancyScorer = mastra.getScorerByName("Answer Relevancy");

const weatherAgent = mastra.getAgent("weatherAgent");

// Use the scorer to evaluate an AI output
await weatherAgent.generate("What is the weather in Rome", {
scorers: {
answerRelevancy: {
scorer: relevancyScorer,
},
},
});

Parameters

name:

string
The name property of the scorer to retrieve. This should match the 'name' field specified when creating the scorer with createScorer().

Returns

scorer:

MastraScorer
The MastraScorer instance that has the matching name property.

Error Handling

This method throws a MastraError if:

  • No scorer with the specified name is found
  • No scorers are registered with the Mastra instance
try {
const scorer = mastra.getScorerByName("Non-existent Scorer");
} catch (error) {
if (error.id === "MASTRA_GET_SCORER_BY_NAME_NOT_FOUND") {
console.log("Scorer with that name not found");
}
}