filterRun()
Creates a prepareRun function from declarative options. Pass the result to createScorer() to filter messages and limit context size. It also drops unnecessary fields before the scorer pipeline runs.
Use filterRun() for declarative filtering. Write a custom prepareRun function directly when you need imperative logic that filterRun() doesn't cover. See Custom scorers: input filtering for more.
Usage exampleUsage example的直接链接
The following example creates a scorer that only sees tool invocations and text messages, limited to the 20 most recent context messages:
import { createScorer, filterRun } from '@mastra/core/evals'
const toolScorer = createScorer({
id: 'tool-usage',
description: 'Evaluates tool usage patterns',
type: 'agent',
prepareRun: filterRun({
partTypes: ['tool-invocation', 'text'],
maxRememberedMessages: 20,
}),
}).generateScore(({ run }) => {
// run.input.rememberedMessages contains only tool and text messages
// run.output contains only tool and text messages
return 1
})
Filter by tool nameFilter by tool name的直接链接
Keep only messages involving specific tools:
import { createScorer, filterRun } from '@mastra/core/evals'
const fileEditScorer = createScorer({
id: 'file-edit-quality',
description: 'Evaluates file editing patterns',
type: 'agent',
prepareRun: filterRun({
toolNames: ['write_file', 'string_replace_lsp', 'view'],
}),
}).generateScore(({ run }) => {
// Only messages with these tool calls remain
return 1
})
Drop fieldsDrop fields的直接链接
Remove fields the scorer doesn't need:
import { createScorer, filterRun } from '@mastra/core/evals'
const simpleScorer = createScorer({
id: 'response-length',
description: 'Checks response length',
type: 'agent',
prepareRun: filterRun({
dropRequestContext: true,
dropExpectedTrajectory: true,
dropGroundTruth: true,
maxOutputMessages: 5,
}),
}).generateScore(({ run }) => {
return run.output.length > 0 ? 1 : 0
})
ParametersParameters的直接链接
options:
partTypes?:
toolNames?:
maxRememberedMessages?:
maxOutputMessages?:
dropRequestContext?:
dropExpectedTrajectory?:
dropGroundTruth?:
Returns: (run: ScorerRun) => ScorerRun. A function suitable for the prepareRun option on createScorer().
Part typesPart types的直接链接
The partTypes option accepts MastraPartType values. Each value is prefix-matched, so 'data-' matches all data part types.
| Type | Description |
|---|---|
'text' | Text content parts |
'tool-invocation' | Tool call and result parts |
'reasoning' | Chain-of-thought reasoning parts |
'step-start' | Step marker parts |
'image' | Image parts |
'file' | File parts |
'source' | Source reference parts |
'source-document' | Source document parts |
'data-' | All data parts (matches any data-* prefix) |
'data-om-' | Observational memory data parts |
'data-workspace-' | Workspace data parts |
'data-sandbox-' | Sandbox data parts |
'data-tool-' | Tool-related data parts |
Filtering behaviorFiltering behavior的直接链接
- Part type filtering applies to both
input.rememberedMessagesandoutputwhen they contain agent message arrays. - Tool name filtering only affects messages that contain tool invocations. Messages without tool invocations are filtered by
partTypesonly, and pass through whenpartTypesisn't set. - System messages (
systemMessages,taggedSystemMessages) are never filtered, regardless ofpartTypesortoolNames. - Message limits (
maxRememberedMessages,maxOutputMessages) apply after type and tool filtering, taking the most recent messages. - When both
partTypesandtoolNamesare set, a message with tool invocations must satisfy both filters to be kept.