Skip to main content
This page covers error types, serialization helpers, and a few platform-specific entry points that don’t fit in the main reference pages.

Errors

Errors are subclasses of LeapException (LeapError is a type alias for LeapException provided for backward compatibility). The most common subclasses:
  • LeapModelLoadingException β€” problems reading or validating the model bundle.
  • LeapGenerationException β€” unexpected native inference errors.
  • LeapGenerationPromptExceedContextLengthException β€” prompt length exceeded the configured context size.
  • LeapSerializationException β€” JSON encoding/decoding problems on chat history or function calls.
Handle thrown errors with do / catch on the async streams returned by Conversation.generateResponse(...), or downcast with if let err = error as? LeapModelLoadingException { ... } to inspect a specific subclass.

Serialization

ChatMessage, ChatMessageContent, LeapFunctionCall, and Manifest are serializable on every platform and round-trip cleanly into JSON compatible with OpenAI’s chat-completions schema.
Use Conversation.exportToJSON() to get an OpenAI-shaped JSON string, then route restores back through Kotlin’s serializer (there is no ChatMessage(from: [String: Any]) initializer):
Persist data to disk, UserDefaults, or your sync backend. On restore, decode the JSON via Kotlin’s LeapJson (re-exported through SKIE) into a [ChatMessage] and rebuild via modelRunner.createConversationFromHistory(history:). There is no Swift-native dictionary-based ChatMessage initializer β€” the Kotlin serializer is the source of truth on both platforms.

Android LeapModelDownloader internals

This section is Android-only. iOS / macOS callers use the Swift ModelDownloader (shipped in the LeapModelDownloader SPM product), which routes transfers through URLSession β€” see Model Loading β†’ Constructing the downloader for background-session configuration. The cross-platform LeapDownloader (used directly on JVM, Linux native, Windows native) is a plain async fetcher with no platform background-service hooks.
Beyond the high-level loadModel / loadSimpleModel methods covered in Model Loading, the Android LeapModelDownloader exposes a few lower-level methods for WorkManager background staging and status polling.

Permission setup

requestDownloadModel(...) enqueues a WorkManager download worker. During transfer, the worker runs in the foreground and displays notifications, so declare these in your AndroidManifest.xml:
On Android 13+ (API 33) request POST_NOTIFICATIONS at runtime:

Status polling API

Refer to the nested status type as LeapModelDownloader.ModelDownloadStatus.NotOnLocal / .DownloadInProgress / .Downloaded on Android β€” the Android downloader does not expose a top-level ai.liquid.leap.downloader.ModelDownloadStatus. (Apple ships a top-level ai.liquid.leap.downloader.ModelDownloadStatus sealed interface with a different payload β€” DownloadInProgress(progress: Double) and a data object Downloaded with no size β€” so don’t share status-decoding code unmodified across platforms.)
  • requestDownloadModel β€” suspend fire-and-forget prefetch. It enqueues a unique WorkManager download worker; the download itself survives app restarts, and the call returns after staging the work request.
  • requestStopDownload β€” suspend; cancels an in-flight background download.
  • queryStatus β€” suspend one-shot status check.
  • observeDownloadProgress β€” StateFlow<ModelDownloadProgress?> for UI updates during a background download. It emits null when no download is active.
  • getModelResourceFolder β€” the directory the SDK will use for this model+quantization on disk.
  • requestStopService β€” @Deprecated no-op since v0.10.6 (WorkManager handles the worker lifecycle automatically). Kept for source compatibility; new code shouldn’t call it.

Removing a downloaded model

Use the Android downloader’s resource folder to clean up disk, or construct a cross-platform LeapDownloader with the same saveDir and call its instance method deleteModelResources(...):

Putting it together

A minimal end-to-end snippet exercising load β†’ conversation β†’ tool registration β†’ constrained generation β†’ streaming.
See also: Quick Start, Function Calling, Constrained Generation.