Workflow.classifier()
The .classifier() method adds a configured Classifier as a declarative workflow step and returns the workflow for chaining. The step outputs a JSON-safe { answers, usage } object that can be consumed by later steps and existing control-flow methods such as .branch().
Usage exampleUsage example的直接链接
const workflow = createWorkflow({
id: 'ticket-triage',
inputSchema: z.object({ message: z.string() }),
outputSchema: z.any(),
})
.map({ message: { initData: true, path: 'message' } })
.classifier(router)
.branch([
[async ({ inputData }) => inputData.answers.route.choice === 'billing', billingStep],
[async ({ inputData }) => inputData.answers.route.choice === 'support', supportStep],
[async () => true, fallbackStep],
])
.commit()
When a classifier instance is passed, question keys and choice literals are inferred in downstream steps. The classifier must contain constructor-configured questions.
ParametersParameters的直接链接
classifierOrId:
options?:
stepOptions?:
ReturnsReturns的直接链接
workflow:
Input mappingInput mapping的直接链接
The classifier evaluates the complete previous step output. Insert .map() before .classifier() to select or reshape its input.
workflow
.map({
message: { initData: true, path: 'message' },
locale: { initData: true, path: 'locale' },
})
.classifier(router)
Mappings use the same validated path grammar as other workflow steps and persist in serialized workflow graphs.
OutputOutput的直接链接
- Choice answers expose
answers.<question>.choiceand may includeprobabilities. - Score answers expose
answers.<question>.scoreand may includeprobabilities. - Boolean answers expose the raw
P(true)value asanswers.<question>.probability. Apply an explicit threshold when routing. - Refused questions are
{ type: 'refusal' }and have nochoice,score, orprobability. usagecontains normalized token usage.
Retries and errorsRetries and errors的直接链接
maxRetries controls retries of the classifier's model call. retries controls retries of the workflow step. The workflow abort signal is forwarded to the classifier. Classifier errors fail the step. .classifier() doesn't apply fail-open behavior.
Referencing a classifier by IDReferencing a classifier by ID的直接链接
Register the classifier on the same Mastra instance that runs the workflow:
export const mastra = new Mastra({
classifiers: { router },
workflows: { workflow },
})
For a typed string reference, supply the configured question type explicitly:
workflow.classifier<typeof router.questions>('router')