Create a custom stream to control the streaming format and structure of tool calls instead of using the built-in AI SDK data stream format (toUIMessageStream()).
fullStream (on StreamTextResult) gives you direct access to all model events. You can transform, filter, and structure these events into your own streaming format. This gives you the benefits of the AI SDK's unified provider interface without prescribing how you consume the stream.
You can:
Define your own stream chunk format
Control how steps and tool calls are structured
Parse the stream manually on the client
Build custom UI from your stream data
For complete control over both the streaming format and the execution loop, combine this pattern with a manual agent loop.
The route uses streamText to process the prompt with tools. Each event (text, tool calls, tool results) is encoded as a Server-Sent Event with a data: prefix and sent to the client.
The client uses the Fetch API to stream responses from the server. Since the server sends Server-Sent Events (newline-delimited with data: prefix), the client:
Reads chunks from the stream using getReader()
Decodes the binary chunks to text
Splits by newlines to identify complete events
Removes the data: prefix and parses the JSON, then appends it to the events list
Events are rendered in order as they arrive, giving you a linear representation of the AI's response.