How Streaming Works
The SDK has one streaming method.send(_:conversationId:configure:) is an async throws function. It calls your callbacks as text arrives, then returns the finished ChatResponse when the reply is complete.
- While it runs: your
onTextDelta,onToolCall, andonToolResultcallbacks fire. - When it succeeds: it returns a
ChatResponse. - When it fails: it throws.
send
ChatResponse.
String
required
The message to send to the agent.
String?
Continue an existing conversation.
nil always starts a new conversation.@Sendable (inout StreamCallbacks) -> Void
A closure where you set the callbacks you want. Set only the ones you need.
StreamCallbacks
Callbacks you do not set are ignored. There is no
onStart, onFinish, or onError callback. The async throws signature covers all three:
Showing text as it arrives
ChatViewModel is marked @MainActor, so await self?.append(chunk) moves to the main thread for you.
Stopping a Reply
Cancelling theTask around send closes the connection.
Cancelling stops your app from reading the reply. The agent may still finish on the server, and the partial message is saved. The text you already received is still correct, and it will be there when you next call
listMessages.Continuing a Conversation
The client remembers the last conversation incurrentConversationId, but you have to pass it back in to continue:
nil (the default), or clear the saved ID:
retry
send.
String
required
The conversation the message belongs to.
String
required
The ID of the agent message to redo.
@Sendable (inout StreamCallbacks) -> Void
The same callbacks as
send.The old message and everything after it are replaced. Remove those messages from your UI before the new reply starts.
ConversationState.retry(messageId:) does this for you.sendNonStreaming
String
required
The message to send to the agent.
String?
Continue an existing conversation.
nil starts a new one.sendNonStreaming when you do not need to show text as it arrives, such as a background summary or a scripted first message.
Which Method to Use
Related
Client-Side Tools
Let the agent run functions on the device
Streaming Types
StreamCallbacks, ToolCallInfo, and more
ChatResponse
What a finished reply contains
Error Handling
Error types and how to handle them
