Skip to main content

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 example
Direct 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 })
}

Parameters
Direct 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`.

Returns
Direct link to Returns

ReadableStream — A stream of AI SDK UIMessage data parts containing:

  • A start marker
  • A WorkflowDataPart with the overall workflow status and all step summaries
  • A WorkflowStepDataPart for each step with its full output
  • A finish marker

Pass this stream to createUIMessageStreamResponse() or createUIMessageStream() from the AI SDK.

On this page