Run.streamVNext()
The .streamVNext()
method enables real-time streaming of responses from a workflow.
Usage example
const run = await workflow.createRunAsync();
const stream = run.streamVNext({
inputData: {
value: "initial data",
},
});
Parameters
inputData?:
z.infer<TInput>
Input data that matches the workflow's input schema
runtimeContext?:
RuntimeContext
Runtime context data to use during workflow execution
Returns
stream:
MastraWorkflowStream<ChunkType>
A custom stream that extends ReadableStream<ChunkType> with additional workflow-specific properties
stream.status:
Promise<RunStatus>
A promise that resolves to the current workflow run status
stream.result:
Promise<WorkflowResult<TOutput, TSteps>>
A promise that resolves to the final workflow result
stream.usage:
Promise<{ promptTokens: number; completionTokens: number; totalTokens: number }>
A promise that resolves to token usage statistics
Extended usage example
const run = await workflow.createRunAsync();
const stream = run.streamVNext({
inputData: {
value: "initial data",
},
});
const result = await stream.result;
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 beginsstep-start
: A step begins executionstep-output
: Custom output from a stepstep-result
: A step completes with resultsfinish
: Workflow execution completes with usage statistics