Skip to main content

Workflow Class

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

Usage example

import { createWorkflow } from "@mastra/core/workflows";
import { z } from "zod";

export const workflow = createWorkflow({
id: "test-workflow",
inputSchema: z.object({
value: z.string(),
}),
outputSchema: z.object({
value: z.string(),
}),
});

Constructor parameters

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

stateSchema?:

z.ZodObject<any>
Optional Zod schema for the workflow state. Automatically injected when using Mastra's state system. If not specified, type is 'any'.

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

Extended usage example

import { mastra } from "./mastra";

const run = await mastra.getWorkflow("workflow").createRunAsync();

const result = await run.start({...});

if (result.status === "suspended") {
const resumedResult = await run.resume({...});
}