Workflow.map()
The .map() method maps output data from a previous step to the input of a subsequent step, allowing you to transform data between steps.
Usage exampleDirect link to Usage example
workflow.map(async ({ inputData }) => `${inputData.value} - map`
ParametersDirect link to Parameters
mappingFunction:
ReturnsDirect link to Returns
workflow:
Using inputDataDirect link to using-inputdata
Use inputData to access the full output of the previous step.
.then(step1)
.map(({ inputData }) => {
console.log(inputData);
})
Using getStepResult()Direct link to using-getstepresult
Use getStepResult() to access the full output of a specific step by referencing the step's instance.
.then(step1)
.map(async ({ getStepResult }) => {
console.log(getStepResult(step1));
})
Using getInitData()Direct link to using-getinitdata
Use getInitData() to access the initial input data provided to the workflow.
.then(step1)
.map(async ({ getInitData }) => {
console.log(getInitData());
})
Using mapVariable()Direct link to using-mapvariable
The object form of .map() provides an alternative declarative syntax for mapping fields. Instead of writing a function, you define an object where each key is a new field name and each value uses mapVariable() to extract data from previous steps or workflow input.
Import mapVariable() from the workflows module:
import { mapVariable } from "@mastra/core/workflows";
Extracting fields from step outputsDirect link to Extracting fields from step outputs
Use mapVariable() with step to extract a specific field from a step's output and map it to a new field name. The path parameter specifies which field to extract.
In this example, the value field from step1's output is extracted and mapped to a new field called details:
.then(step1)
.map({
details: mapVariable({
step: step1,
path: "value"
})
})
Extracting fields from workflow inputDirect link to Extracting fields from workflow input
Use mapVariable() with initData to extract a specific field from the workflow's initial input data. This is useful when you need to pass the original workflow input to a later step.
In this example, the value field from the workflow's input is extracted and mapped to a field called details:
export const testWorkflow = createWorkflow({...});
testWorkflow
.then(step1)
.map({
details: mapVariable({
initData: testWorkflow,
path: "value"
})
})