Xybrid
SDKs

Kotlin

Kotlin SDK patterns and advanced usage

The Kotlin SDK (ai.xybrid:xybrid-kotlin) is published on Maven Central and uses UniFFI-generated bindings for type-safe ML inference from Kotlin. For installation and core API reference, see the Android SDK guide.

This page covers Kotlin-specific patterns and advanced usage.

Sealed Classes

The SDK leverages Kotlin sealed classes for exhaustive pattern matching.

Envelope Variants

val envelope: XybridEnvelope = when (inputType) {
    InputType.AUDIO -> Envelope.audio(audioBytes, 16000u)
    InputType.TEXT -> Envelope.text("Hello world")
    InputType.EMBEDDING -> Envelope.embedding(floatList)
}

Error Handling

val result = try {
    model.run(envelope)
} catch (e: XybridException) {
    when (e) {
        is XybridException.ModelNotFound -> showError("Model ${e.modelId} not found")
        is XybridException.InferenceFailed -> showError("Failed: ${e.message}")
        is XybridException.InvalidInput -> showError("Bad input: ${e.message}")
        is XybridException.IoException -> showError("I/O error: ${e.message}")
    }
    return
}

Coroutines Integration

Wrap synchronous SDK calls with coroutines for non-blocking UI:

import kotlinx.coroutines.*

class InferenceRepository {
    private val dispatcher = Dispatchers.IO

    suspend fun loadModel(modelId: String): XybridModel = withContext(dispatcher) {
        XybridModelLoader.fromRegistry(modelId).load()
    }

    suspend fun runInference(
        model: XybridModel,
        envelope: XybridEnvelope
    ): XybridResult = withContext(dispatcher) {
        model.run(envelope)
    }
}

Jetpack Compose Integration

@Composable
fun InferenceScreen() {
    var result by remember { mutableStateOf<XybridResult?>(null) }
    var isLoading by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()

    Button(onClick = {
        scope.launch {
            isLoading = true
            val model = withContext(Dispatchers.IO) {
                XybridModelLoader.fromRegistry("kokoro-82m").load()
            }
            result = withContext(Dispatchers.IO) {
                model.run(Envelope.text("Hello from Compose!"))
            }
            isLoading = false
        }
    }) {
        Text(if (isLoading) "Running..." else "Run Inference")
    }

    result?.let {
        Text("Output: ${it.text ?: "Audio output"}")
        Text("Latency: ${it.latencyMs}ms")
    }
}

Type Aliases

The SDK provides short aliases matching the full qualified names:

AliasFull Type
ModelLoaderXybridModelLoader
ModelXybridModel
EnvelopeXybridEnvelope
ResultXybridResult
XybridErrorXybridException

See the Android SDK guide for the complete API reference.

On this page