createUIMessageStream
API Reference for 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
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 onFinish outcome failed. Individual error chunks do not change
the outcome by themselves. When merging multiple child streams, aggregate
their outcomes and call setOutcome once.
API Signature
Parameters
execute:
write:
merge:
setOutcome:
onError:
onError:
originalMessages:
onFinish:
messages:
isContinuation:
isAborted:
outcome:
responseMessage:
generateId:
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.