Conditional Branching
Workflows often need to follow different paths based on a condition. These examples demonstrate how to use .branch()
to create conditional flows using both steps and workflows.
Conditional logic using steps
In this example, the workflow uses .branch()
to execute one of two steps based on a condition. If the input value
is less than or equal to 10, it runs lessThanStep
and returns 0
. If the value is greater than 10, it runs greaterThanStep
and returns 20
. Only the first matching branch is executed, and its output becomes the output of the workflow.
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const lessThanStep = createStep({
id: "less-than-step",
description: "if value is <=10, return 0",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
}),
execute: async () => {
return {
value: 0
};
}
});
const greaterThanStep = createStep({
id: "greater-than-step",
description: "if value is >10, return 20",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
}),
execute: async () => {
return {
value: 20
};
}
});
export const branchSteps = createWorkflow({
id: "branch-workflow",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
})
})
.branch([
[async ({ inputData: { value } }) => value <= 10, lessThanStep],
[async ({ inputData: { value } }) => value > 10, greaterThanStep]
])
.commit();
Run this example with an input value of less than or greater than 10.
Conditional logic using workflows
In this example, the workflow uses .branch()
to execute one of two nested workflows based on a condition. If the input value
is less than or equal to 10, it runs lessThanWorkflow
, which runs the lessThanStep
. If the value is greater than 10, it runs greaterThanWorkflow
, which runs the greaterThanStep
.
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const lessThanStep = createStep({
id: "less-than-step",
description: "if value is <=10, return 0",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
}),
execute: async () => {
return {
value: 0
};
}
});
const greaterThanStep = createStep({
id: "greater-than-step",
description: "if value is >10, return 20",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
}),
execute: async () => {
return {
value: 20
};
}
});
export const lessThanWorkflow = createWorkflow({
id: "less-than-workflow",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
})
})
.then(lessThanStep)
.commit();
export const greaterThanWorkflow = createWorkflow({
id: "greater-than-workflow",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
})
})
.then(greaterThanStep)
.commit();
export const branchWorkflows = createWorkflow({
id: "branch-workflow",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
})
})
.branch([
[async ({ inputData: { value } }) => value <= 10, lessThanWorkflow],
[async ({ inputData: { value } }) => value > 10, greaterThanWorkflow]
])
.commit();
Run this example with an input value of less than or greater than 10.
Workflows (Legacy)
The following links provide example documentation for legacy workflows: