Skip to main content

StreamCallbacks

struct StreamCallbacks: Sendable The callbacks you pass to send(_:conversationId:configure:) and retry(conversationId:messageId:configure:).
(@Sendable (String) async -> Void)?
Runs for each chunk of text. Add it to the current bubble.
(@Sendable (ToolCallInfo) async -> Void)?
Runs when the agent asks for a tool, before your handler runs.
(@Sendable (ToolResultInfo) async -> Void)?
Runs when a tool result is ready, whether it came from your handler or from the server.
Callbacks you do not set are ignored:
There is no onStart, onFinish, or onError callback. The async throws signature covers all three: the call returns a ChatResponse when it works and throws when it does not. See Streaming.
Callbacks do not run on the main thread. Use await MainActor.run { ... }, or call into a @MainActor type, before touching your UI. The SDK waits for each callback before reading more of the reply, so keep them quick.

ToolCallInfo

struct ToolCallInfo: Sendable Passed to onToolCall.
String
required
An ID for this tool call. Use it to match this call with its ToolResultInfo.
String
required
The tool’s name, which matches the Custom Action on your agent.
JSONValue
required
Everything the agent passed to the tool. See JSONValue.

ToolResultInfo

struct ToolResultInfo: Sendable Passed to onToolResult.
String
required
Matches the toolCallId on the ToolCallInfo it belongs to.
String
required
The tool’s name.
JSONValue
required
What the tool returned. An object with an error key means it failed. See JSONValue.
Both callbacks also run for tools the server handles, not just your own, so one piece of UI can show every tool the agent uses.

ToolHandler

The shape of a tool handler you register with client.tool(_:handler:). See Client-Side Tools.

Lower-Level Types

The types below are the pieces a reply is made of. The SDK handles them for you and gives you the results through StreamCallbacks and ChatResponse, so you do not work with them directly. They are public so you can read what a reply contains and name the types in your own code.
There is no way to read these events as they arrive. ChatbaseClient has no Combine publisher and no AsyncSequence, so there is no equivalent of Android’s sendMessageStream. Use send with callbacks. If the callbacks do not give you what you need, contact support.

StreamEvent

ToolCall

The raw tool call inside .toolCall. ToolCallInfo is the version you get in callbacks.

StreamFinishInfo

The details that arrive when a reply ends. Everything is optional, since the server may leave any of it out.
String?
The conversation this reply belongs to. On a new conversation, this is where the ID first arrives.
String?
The agent’s final message ID.
String?
The server’s ID for the user’s message.
String?
Who the reply belongs to. This fills in client.currentUserId.
FinishReason?
See FinishReason. Treated as .stop when missing.
Usage?
Credits used. Treated as 0 when missing.
These become the matching fields on the ChatResponse that send returns.
Anything in a reply that the SDK does not recognize is skipped rather than treated as an error, so a shipped app keeps working as Chatbase adds new capabilities. A reply that cannot be read at all ends with ChatError.decodingFailed.

Streaming

Using the callbacks in practice

Client-Side Tools

Registering tools the agent can run

ChatResponse

What a finished reply contains

JSONValue

Reading tool data