Skip to main content

Two-Tier Streaming API

The SDK provides two levels of abstraction for streaming:
  • sendMessage() — High-level API with a callback DSL. Handles tool calls automatically (up to 10 iterations). Recommended for most use cases.
  • sendMessageStream() — Low-level API returning a Flow<ChatStreamEvent>. Tool calls are not handled automatically, giving you full control over event processing.

sendMessage

interface ChatbaseClientPackage: com.chatbase.sdk
Sends a message, streams the response in real time, and automatically handles tool calls (up to 10 iterations). Returns the aggregated ChatResponse when the stream completes.
String
required
The user message to send to the agent.
String?
Continue an existing conversation. Omit to use currentConversationId or start a new one.
StreamCallbacks.() -> Unit
Streaming callback DSL.

StreamCallbacks

class StreamCallbacksPackage: com.chatbase.sdk
All callbacks are invoked on Dispatchers.Main — it is safe to update UI directly from any callback without explicit dispatching.
Called when the connection opens and streaming begins.
Called for each incremental text chunk received.
Called when a tool call’s full input is available (before execution).
Called after a tool handler executes and returns a result.
Called when the stream completes successfully.
Called when an error occurs during streaming.

ChatResponse

data class ChatResponsePackage: com.chatbase.sdk.model The aggregated result after streaming completes.
String
required
Server-assigned message ID.
String
required
Always "assistant".
List<Part>
required
The response content — text, tool calls, and tool results. See ChatResponse.
ResponseMetadata
required

sendMessageStream

Returns a cold Flow of raw streaming events. Tool calls are not handled in Flow mode — registered tool handlers are not invoked, and the SDK exposes no API for submitting tool results manually.
String
required
The user message to send to the agent.
String?
Continue an existing conversation. Omit to use currentConversationId or start a new one.
Tool calls are not executed in Flow mode — tool events are informational only, and the stream finishes with finishReason == "tool-calls" without a final answer. If your agent uses client-side tools, use sendMessage with callbacks instead. See Client-Side Tools for details.
You can also filter for specific event types:

Stream Events

The Flow returned by sendMessageStream emits ChatStreamEvent objects — text deltas, tool input/output, step lifecycle, and errors. See Streaming Events for the full type reference.

Continuing a Conversation

The SDK automatically tracks the current conversation. After sending a message, subsequent calls reuse the same conversation:
To start a fresh conversation:
See Conversations & History for listing conversations and loading message history.

retry

Retry a failed assistant message. Same streaming and tool-loop behavior as sendMessage.
String
required
The conversation containing the failed message.
String
required
The ID of the assistant message to retry.
StreamCallbacks.() -> Unit
Streaming callback DSL.
A convenience extension extracts the IDs from a ChatResponse:

retryStream

Raw streaming variant of retry. Tool calls are not handled automatically.

Client-Side Tools

Register tool handlers the agent can invoke

Error Handling

Exception hierarchy and error handling patterns