Skip to Content
ReferencevNext WorkflowsWorkflow

Workflow Class

The Workflow class enables you to create state machines for complex sequences of operations with conditional branching and data validation.

Usage

const myWorkflow = createWorkflow({ id: "my-workflow", inputSchema: z.object({ startValue: z.string(), }), outputSchema: z.object({ result: z.string(), }), steps: [step1, step2, step3], // Declare steps used in this workflow }) .then(step1) .then(step2) .then(step3) .commit(); const mastra = new Mastra({ vnext_workflows: { myWorkflow, }, }); const run = mastra.vnext_getWorkflow("myWorkflow").createRun();

API Reference

Constructor

id:

string
Unique identifier for the workflow

inputSchema:

z.ZodType<any>
Zod schema defining the input structure for the workflow

outputSchema:

z.ZodType<any>
Zod schema defining the output structure for the workflow

steps:

Step[]
Array of steps to include in the workflow

Core Methods

then()

Adds a step to the workflow sequentially. Returns the workflow instance for chaining.

parallel()

Executes multiple steps concurrently. Takes an array of steps and returns the workflow instance.

branch()

Creates conditional branching logic. Takes an array of tuples containing condition functions and steps to execute when conditions are met.

dowhile()

Creates a loop that executes a step repeatedly while a condition remains true. The condition is checked after each execution.

dountil()

Creates a loop that executes a step repeatedly until a condition becomes true. The condition is checked after each execution.

foreach()

Iterates over an array and executes a step for each element. Accepts optional concurrency configuration.

map()

Maps data between steps using either a mapping configuration object or a mapping function. Useful for transforming data between steps.

commit()

Validates and finalizes the workflow configuration. Must be called after adding all steps.

createRun()

Creates a new workflow run instance, allowing you to execute the workflow with specific input data. Accepts optional run ID.

execute()

Executes the workflow with provided input data. Handles workflow suspension, resumption and emits events during execution.

Workflow Status

A workflow’s status indicates its current execution state. The possible values are:

success:

string
All steps finished executing successfully, with a valid result output

failed:

string
Workflow encountered an error during execution, with error details available

suspended:

string
Workflow execution is paused waiting for resume, with suspended step information

Passing Context Between Steps

Steps can access data from previous steps in the workflow through the context object. Each step receives the accumulated context from all previous steps that have executed.

workflow .then({ id: "getData", execute: async ({ inputData }) => { return { data: { id: "123", value: "example" }, }; }, }) .then({ id: "processData", execute: async ({ inputData }) => { // Access data from previous step through context.steps const previousData = inputData.data; // Process previousData.id and previousData.value }, });