Skip to main content

Workflow.classifier()

The .classifier() method adds a configured Classifier as a declarative workflow step. It returns a JSON-safe { answers, usage } object that can be consumed by later steps and existing control-flow methods such as .branch().

Usage example
Direct link to 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
Direct link to 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
Direct link to Returns

workflow:

Workflow
The workflow instance for method chaining

Input mapping
Direct link to 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
Direct link to 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.
  • usage contains normalized token usage.

Retries and errors
Direct link to 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
Direct link to 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')