Skip to main content

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 example

workflow.map(async ({ inputData }) => `${inputData.value} - map`

Parameters

mappingFunction:

(params: { inputData: any }) => any
Function that transforms input data and returns the mapped result

Returns

workflow:

Workflow
The workflow instance for method chaining

Using inputData

Use inputData to access the full output of the previous step.

  .then(step1)
.map(({ inputData }) => {
console.log(inputData);
})

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()

Use getInitData() to access the initial input data provided to the workflow.

  .then(step1)
.map(async ({ getInitData }) => {
console.log(getInitData());
})

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 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 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"
})
})