Skip to main content

Run.stream()

The .stream() method allows you to monitor the execution of a workflow run, providing real-time updates on the status of steps.

Usage exampleDirect link to Usage example

const run = await workflow.createRunAsync();

const { stream } = await run.stream({
inputData: {
value: "initial data",
},
});

ParametersDirect link to Parameters

inputData?:

z.infer<TInput>
Input data that matches the workflow's input schema

runtimeContext?:

RuntimeContext
Request Context data to use during workflow execution

tracingContext?:

TracingContext
AI tracing context for creating child spans and adding metadata. Automatically injected when using Mastra's tracing system.

currentSpan?:

AISpan
Current AI span for creating child spans and adding metadata. Use this to create custom child spans or update span attributes during execution.

tracingOptions?:

TracingOptions
Options for AI tracing configuration.

metadata?:

Record<string, any>
Metadata to add to the root trace span. Useful for adding custom attributes like user IDs, session IDs, or feature flags.

ReturnsDirect link to Returns

stream:

ReadableStream<StreamEvent>
A readable stream that emits workflow execution events in real-time

getWorkflowState:

() => Promise<WorkflowResult<TState, TOutput, TSteps>>
A function that returns a promise resolving to the final workflow result

traceId?:

string
The trace ID associated with this execution when AI tracing is enabled. Use this to correlate logs and debug execution flow.

Extended usage exampleDirect link to Extended usage example

const { getWorkflowState } = await run.stream({
inputData: {
value: "initial data",
},
});

const result = await getWorkflowState();

Stream EventsDirect link to Stream Events

The stream emits various event types during workflow execution. Each event has a type field and a payload containing relevant data:

  • start: Workflow execution begins
  • step-start: A step begins execution
  • tool-call: A tool call is initiated
  • tool-call-streaming-start: Tool call streaming begins
  • tool-call-delta: Incremental tool output updates
  • step-result: A step completes with results
  • step-finish: A step finishes execution
  • finish: Workflow execution completes

On this page