The createUIMessageStream function allows you to create a readable stream for UI messages with advanced features like message merging, error handling, and finish callbacks.
const existingMessages: UIMessage[] = [
/* ... */
];
const stream = createUIMessageStream({
async execute({ writer }) {
// The outer stream owns the assistant message lifecycle.
writer.write({ type: 'start' });
// 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-5.5",
prompt: 'Write a haiku about AI',
});
writer.merge(
toUIMessageStream({
stream: result.stream,
sendStart: false,
onEnd: ({ outcome }) => {
// The composer decides that the model stream outcome is also the
// aggregate stream outcome.
writer.setOutcome(outcome);
},
}),
);
},
onError: error =>
`Custom error: ${error instanceof Error ? error.message : String(error)}`,
originalMessages: existingMessages,
onEnd: ({ messages, isContinuation, outcome, responseMessage }) => {
console.log('Stream ended with messages:', messages);
console.log('Stream outcome:', outcome.status);
},
});
setOutcome records the composer's policy without writing a chunk or closing
the stream. The first outcome declared through setOutcome is retained, but a
fatal execution, merge, error-handling, or downstream processing failure makes
the final onEnd outcome failed. Individual error chunks do not change the
outcome by themselves. When merging multiple child streams, aggregate their
outcomes and call setOutcome once.
Declares the operation-level outcome of the composed stream. The first outcome declared through this method is retained, while fatal execution, merge, error-handling, or downstream processing failures override declarations. Supported statuses are 'completed', 'failed', 'aborted', and 'unknown'. Declaring an outcome does not write a chunk or close the stream.
A function that handles errors and returns an error message string. By default, it returns "An error occurred." so server-side error details are not sent to the client.
The operation-level outcome of the stream. It reflects the stream owner declaration unless a fatal stream-processing failure occurs, and is separate from model finish reasons and individual error chunks. It remains 'unknown' when the consumer cancels before an outcome is declared; check isCancelled to distinguish that case from normal closure without a declared outcome.
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.