Skip to main content

Overview

The VLM (Vision Language Model) module enables on-device multimodal inference — feed an image and a text prompt to get descriptions, answers, or analysis. VLM runs entirely on the device using llama.cpp with no data leaving the phone.

Package Imports

Model registration types come from the core SDK:

Basic Usage

processImageStream returns a Flow<String> where each emission is a single token, enabling real-time streaming display.

Model Setup

Register a VLM Model

VLM models require two files: the main model GGUF and a multimodal projector (mmproj) GGUF. Register them using registerMultiFileModel:

Load and Unload

unloadVLMModel() can throw if the model is not currently loaded. Always wrap it in a try/catch block.

API Reference

VLMImage

VLMImage.fromFilePath() requires a file system path, not a content URI. If you’re using Android’s photo picker or ACTION_OPEN_DOCUMENT, you must first copy the image to a temporary file. See the image picker example below.

VLMGenerationOptions

processImageStream

Returns a Flow<String> that emits one token at a time. Collect the flow to build up the full response incrementally.

cancelVLMGeneration

Cancels the current VLM generation. Safe to call even if no generation is in progress.

Model State

isVLMModelLoaded is a property, not a suspend function. Unlike other model checks in the SDK (e.g., isLLMModelLoaded()), you access it directly without calling it.

Model Registration

Examples

Jetpack Compose with Image Picker

A complete example showing image selection, VLM processing, and streaming token display:
The saveUriToTempFile helper is essential when using Android’s photo picker. Content URIs from PickVisualMedia cannot be passed directly to VLMImage.fromFilePath() — you must first write the image to a temporary JPEG file on disk.

Batch Image Description

Error Handling

Supported Models

Performance Tips

  • Start with SmolVLM 256M — smallest multimodal model, fast and memory-efficient - Limit maxTokens — use 50-100 for quick descriptions, 300+ for detailed analysis - Cancel long generations — call cancelVLMGeneration() if the user navigates away - Unload when idle — VLM models consume significant memory; unload if the user switches to a different feature - Pre-copy images — save content URIs to temp files before starting VLM inference to avoid blocking on I/O during generation

LLM Generation

Text-only generation

LLM Streaming

Streaming text generation

Tool Calling

Function calling with LLMs

Best Practices

Performance optimization