跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

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 example
Usage 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.

Parameters
Parameters的直接链接

classifierOrId:

Classifier<QUESTIONS> | string
A configured classifier instance, or the ID of a classifier registered on the Mastra instance. String references are resolved at execution time.

options?:

ClassifierStepOptions
Classifier and step options, including model-call maxRetries, providerOptions, workflow retries, metadata, and an optional id.

stepOptions?:

{ id?: string }
The step's call-site ID within the workflow. Defaults to the classifier ID. This value takes precedence over options.id.

Returns
Returns的直接链接

workflow:

Workflow
The workflow instance for method chaining

Input mapping
Input 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.

Output
Output的直接链接

  • Choice answers expose answers.<question>.choice and may include probabilities.
  • Score answers expose answers.<question>.score and may include probabilities.
  • Boolean answers expose the raw P(true) value as answers.<question>.probability. Apply an explicit threshold when routing.
  • Refused questions are { type: 'refusal' } and have no choice, score, or probability.
  • usage contains normalized token usage.

Retries and errors
Retries 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 ID
Referencing 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')