createUIMessageStream
The createUIMessageStream function allows you to create a readable stream for UI messages with advanced features like message merging, error handling, and finish callbacks.
Import
import { createUIMessageStream } from "ai"Example
const existingMessages: UIMessage[] = [ /* ... */];
const stream = createUIMessageStream({ async execute({ writer }) { // Start a text message // Note: The id must be consistent across text-start, text-delta, and text-end steps // This allows the system to correctly identify they belong to the same text block writer.write({ type: 'text-start', id: 'example-text', });
// Write a message chunk writer.write({ type: 'text-delta', id: 'example-text', delta: 'Hello', });
// End the text message writer.write({ type: 'text-end', id: 'example-text', });
// Merge another stream from streamText const result = streamText({ model: "anthropic/claude-sonnet-4.5", prompt: 'Write a haiku about AI', });
writer.merge(result.toUIMessageStream()); }, onError: error => `Custom error: ${error.message}`, originalMessages: existingMessages, onFinish: ({ messages, isContinuation, responseMessage }) => { console.log('Stream finished with messages:', messages); },});API Signature
Parameters
execute:
(options: { writer: UIMessageStreamWriter }) => Promise<void> | void
UIMessageStreamWriter
write:
(part: UIMessageChunk) => void
merge:
(stream: ReadableStream<UIMessageChunk>) => void
onError:
(error: unknown) => string
onError:
(error: unknown) => string
originalMessages:
UIMessage[] | undefined
onFinish:
(options: { messages: UIMessage[]; isContinuation: boolean; responseMessage: UIMessage }) => void | undefined
FinishOptions
messages:
UIMessage[]
isContinuation:
boolean
responseMessage:
UIMessage
generateId:
IdGenerator | undefined
Returns
ReadableStream<UIMessageChunk>
A readable stream that emits UI message chunks. The stream automatically handles error propagation, merging of multiple streams, and proper cleanup when all operations are complete.