Error Handling
All errors are thrown as LeapException, which has following subclasses:
LeapModelLoadingException : error in loading the model
LeapGenerationException : error in generating content
LeapGenerationPromptExceedContextLengthException: the prompt text exceeds the maximum context length so no content will be generated
LeapSerializationException : error in serializing / deserializing data.
Serialization Support
The LEAP SDK uses kotlinx.serialization for JSON serialization and deserialization. This is built into the core SDK and requires no additional dependencies.
The following types are @Serializable:
Serializing and Deserializing Conversation History
Add kotlinx.serialization to your project:
Save conversation history:
Restore conversation history:
Serialize single message:
LeapModelDownloader (Android)
LeapModelDownloader is the recommended option for Android applications. It provides background downloads with WorkManager, foreground service notifications, and robust handling of network interruptions.
The LeapSDK Android Model Downloader module is a production-ready helper for downloading models from the LEAP Model Library on Android. It runs as a foreground service and displays notifications to users during downloads.
Permission Setup
The model downloader requires notification permissions to display download progress. You need to:
- Add permissions to AndroidManifest.xml:
- Request notification permission at runtime (Android 13+):
Installation
Basic Usage
API Reference
LeapModelDownloader
LeapModelDownloader is the instance to make request of downloading models and to query the status of a model download request.
Constructor parameters
context: The Android context to retrieve cache directory and launch services. The activity context works for this purpose.
modelFileDir: The path to store model files. If it is not set, a path in the app’s external file dir will be used.
extraHTTPRequestHeaders: Any extra HTTP request headers to send when downloading a model.
notificationConfig: Configuration on the content of Android notifications visible to the users.
getModelFile
Return a file object of the model file based on the DownloadableModel instance. The file may not exists.
requestDownloadModel
Make a request to download the model. If the model file already exists locally, it won’t be downloaded.
model: A DownloadableModel instance.
forceDownload: If it’s true, downloader will remove the model bundle file that exists locally and to conduct the download.
requestStopDownload
Make a request to stop downloading a model.
queryStatus
Query the status of the model. The return value is a ModelDownloadStatus object:
There are three possible value types:
NotOnLocal The model file has not been downloaded or has already been deleted.
DownloadInProgress The model file is still being downloaded. totalSizeInBytes is the total size of the file and downloadedSizeInBytes is the size of downloaded portion. If the total size is not available, totalSizeInBytes will be -1.
Downloaded The file has been downloaded. totalSizeInBytes is the file size.
requestStopService
Make a request to stop the foreground service of the model downloader.
DownloadableModel
DownloadableModel is an interface to describe the model can be downloaded by the LeapSDK Model Downloader.
uri: The URI of the model to download.
name: A user-friendly name of the model. It will be displayed in the notification.
localFilename: The filename to store the model bundle file locally.
LeapDownloadableModel
LeapDownloadableModel implements DownloadableModel.
It is designed to download models from Leap Model Library. resolve method is provided to retrieve the model
from Leap Model Library.
The resolve method accepts 2 parameters:
modelSlug: The model slug that identifies the model. It is usually the lowercase string of the model name. For example, the slug of LFM2-1.2B is lfm2-1.2b.
quantizationSlug: The model quantization slug. It can be found in the “Available quantizations” section of the model card.