Streaming events in the Android SDK
Reference for every StreamEvent the Android SDK emits while an answer streams: MessageDelta, MessageDone, ToolCall and Error.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
streamChat emits StreamEvent values. This page lists each case, the server event it comes from and what your app should do with it. For a full collector, see Streaming.
The StreamEvent type
sealed interface StreamEvent {
data class MessageDelta(val text: String) : StreamEvent
data class MessageDone(val message: Message, val conversationId: String) : StreamEvent
data class ToolCall(val callId: String, val name: String, val arguments: JsonObject) : StreamEvent
data class Error(val error: OctoError) : StreamEvent
}
| Case | Server event | Meaning |
|---|---|---|
MessageDelta |
message.delta |
The next piece of the answer text. |
MessageDone |
message.done |
The answer is complete. Carries the final Message and the conversation id. |
ToolCall |
tool.call |
The agent wants to run a client tool in your app. |
Error |
error |
The stream failed after it had started. |
After the server sends its [DONE] marker, the Flow completes. You do not receive a separate event for it.
Event order
A normal answer is a run of MessageDelta events followed by one MessageDone. When the agent calls a tool, a ToolCall can arrive between deltas. The agent then waits for the tool result before it writes the rest of the answer.
Handle each case
MessageDelta
Append text to what you already show. Deltas are plain text pieces and may split a word in the middle, so do not trim or format each one on its own.
MessageDone
Replace the partial text with message.content. Read message.sources to show where the answer came from, and store conversationId if this was the first message of a new thread.
ToolCall
If you registered a matching client tool, the SDK runs it for you and sends the result back. You still receive the event, which is useful for showing a status line such as "Checking your order". If no tool with that name is registered, the SDK reports the failure to the agent so it can answer without it.
Error
An Error event means the answer cannot be completed. Show a retry control and keep the text received so far. The error field is an OctoError, the same type thrown by chat.
Note: Errors that happen before the first byte, such as a bad agent id or no network, are thrown from the
Flowinstead of being emitted asError. Add acatchoperator as well as handling the event.
Related pages
- Chat response for the non-streaming version of the same result.