Skip to content
QwryAI
Documentation
Pages

Android SDK

Streaming

Show your agent's answer as it is written with the QwryAI Android SDK

Two ways to get an answer

The SDK gives you two calls for a new message. Both run your client-side tools for you, up to 10 rounds by default.

  • send() streams the answer and calls your handlers as each piece arrives, then returns the complete ChatResponse. Use it for chat screens.
  • sendNonStreaming() waits and returns the whole answer in one response. Use it when nobody is watching the text appear, such as a background task.

send

class QwryClient, Package: com.qwryai.sdk

Kotlin
suspend fun send(
    text: String,
    conversationId: String? = null,
    handlers: SendHandlers.() -> Unit = {}
): ChatResponse

Sends a message, streams the answer, runs any client-side tools the agent calls, and returns the finished ChatResponse.

text String required

The message to send.

conversationId String?

The conversation to continue. Leave it out to continue currentConversationId, or to start a new conversation when there's none.

handlers SendHandlers.() -> Unit

Handlers for what happens while the answer is written.

Kotlin
val response = client.send("Where is my order?") {
    onTextDelta = { delta ->
        // Each new piece of text, in order
        print(delta)
    }
    onToolCall = { call ->
        println("Agent is calling: ${call.name}")
    }
    onToolResult = { result ->
        println("Tool result: ${result.output}")
    }
}
println("\nDone! Message ID: ${response.message.id}")

SendHandlers

class SendHandlers, Package: com.qwryai.sdk

Kotlin
var onTextDelta: ((String) -> Unit)?

Called with each new piece of the agent's text.

Kotlin
var onToolCall: ((MessagePart.ToolCall) -> Unit)?

Called when the agent calls a tool, before it runs. This covers tools your app registered and actions QwryAI runs on the server.

Kotlin
var onToolResult: ((MessagePart.ToolResult) -> Unit)?

Called when a tool has finished, with what it returned.

There's no start, finish or error handler: send returns the ChatResponse when the answer is complete and throws a QwryException when it fails. See Error Handling.

ChatResponse

data class ChatResponse, Package: com.qwryai.sdk

What send, sendNonStreaming and retry return once the answer is complete.

message Message required

The agent's answer: its id, role (ASSISTANT), parts and text.

conversationId String required

The conversation the answer belongs to. Pass it to continue that conversation.

userMessageId String?

The ID the server gave the message you sent.

userId String?

The identified user, or null while the SDK is anonymous.

finishReason ChatResponse.FinishReason required

Why the answer ended: STOP, ERROR or TOOL_CALLS.

credits Double required

Message credits this answer used.

See ChatResponse for every field.

sendNonStreaming

Kotlin
suspend fun sendNonStreaming(
    text: String,
    conversationId: String? = null
): ChatResponse

Like send, but the server returns the whole answer at once and no handlers run. Client-side tools still run.

text String required

The message to send.

conversationId String?

The conversation to continue. Leave it out to continue currentConversationId or start a new one.

Kotlin
val response = client.sendNonStreaming("Summarise my last order in one line.")
println(response.message.text)

Continuing a conversation

The client remembers the conversation it last used. After a reply, the next call without a conversationId continues it:

Kotlin
// First message: starts a new conversation
client.send("My name is Alice.")
println(client.currentConversationId) // the new conversation's ID

// Later messages continue it
client.send("What is my name?") // The agent remembers: "Alice"

To start a fresh conversation:

Kotlin
client.newConversation()
client.send("Fresh start!") // Creates a new conversation

retry

Kotlin
suspend fun retry(
    conversationId: String,
    messageId: String,
    handlers: SendHandlers.() -> Unit = {}
): ChatResponse

Asks the agent to answer again in place of one of its messages. That answer and everything after it are replaced. Streams and runs tools exactly like send.

conversationId String required

The conversation that holds the answer.

messageId String required

The ID of the agent's message to answer again.

handlers SendHandlers.() -> Unit

The same handlers as send.

Kotlin
val response = client.send("Hello")
// ... later:
val retried = client.retry(response.conversationId, response.message.id) {
    onTextDelta = { delta -> print(delta) }
}

Streaming into Compose

ConversationState keeps one conversation's messages in a StateFlow and grows the agent's answer in place as it streams, so you don't have to write handlers yourself:

Kotlin
@Composable
fun Chat(client: QwryClient) {
    val chat = remember { ConversationState(client) }
    val messages by chat.messages.collectAsState()
    val sending by chat.isSending.collectAsState()
    val scope = rememberCoroutineScope()

    LazyColumn { items(messages, key = { it.id }) { Text(it.text) } }
    Button(enabled = !sending, onClick = { scope.launch { chat.send("Hello") } }) { Text("Send") }
}

chat.retry(messageId) asks for a new answer in place of one shown, and chat.error holds the last failure. See Conversations & History.

Explore the documentation