workflowSnapshotToStream()
Converts a WorkflowState object (as returned by workflow.getWorkflowRunById()) into a ReadableStream of AI SDK UIMessage data parts. The stream contains the same WorkflowDataPart and WorkflowStepDataPart chunks that the live workflowRoute() produces.
Use this when you want to display a completed or suspended workflow run using the same UI components that render live workflow streams.
Usage exampleDirect link to Usage example
Next.js App Router endpoint that replays a past workflow run as a stream:
app/api/workflow-replay/route.ts
import { workflowSnapshotToStream } from '@mastra/ai-sdk'
import { createUIMessageStreamResponse } from 'ai'
import { mastra } from '@/src/mastra'
export async function GET(req: Request) {
const { searchParams } = new URL(req.url)
const runId = searchParams.get('runId')!
const workflowRun = await mastra.getWorkflow('myWorkflow').getWorkflowRunById(runId)
if (!workflowRun) {
return new Response('Not found', { status: 404 })
}
const stream = workflowSnapshotToStream(workflowRun)
return createUIMessageStreamResponse({ stream })
}
ParametersDirect link to Parameters
workflowRun:
WorkflowState
The workflow run state object, as returned by `getWorkflowRunById()` or the workflow runs API. Contains `runId`, `workflowName`, `status`, and `steps`.
ReturnsDirect link to Returns
ReadableStream — A stream of AI SDK UIMessage data parts containing:
- A
startmarker - A
WorkflowDataPartwith the overall workflow status and all step summaries - A
WorkflowStepDataPartfor each step with its full output - A
finishmarker
Pass this stream to createUIMessageStreamResponse() or createUIMessageStream() from the AI SDK.
RelatedDirect link to Related
workflowRoute(): Stream live workflow runshandleWorkflowStream(): Framework-agnostic handler for live workflow streamingtoAISdkStream(): Convert live Mastra streams to AI SDK formattoAISdkMessages(): Convert stored agent messages to AI SDK format