Overview
VLM handles:- Image encoding — Platform-aware constructors for iOS (
UIImage) and macOS (raw RGB pixels) - Model loading — Multi-file GGUF models with multimodal projector
- Streaming generation — Token-by-token output via
AsyncSequence - Cancellation — Interrupt long-running generations at any time
Basic Usage
Setup
Register a VLM Model
VLM models require two GGUF files: the main language model and a multimodal projector (mmproj). Use registerMultiFileModel to register both:
The first file is the main model GGUF, and the second is the multimodal projector. Both are
required for VLM inference.
Load the Model
API Reference
VLMImage
Platform-conditional image wrapper for VLM input.Model Operations
processImageStream
Returns a result object with a
.stream property — an AsyncSequence of String tokens.
registerMultiFileModel
Platform-Specific Image Handling
VLM image creation differs between iOS and macOS. Use conditional compilation to handle both:Examples
Complete SwiftUI App with PhotosPicker
Batch Image Analysis
Error Handling
Best Practices
Use registerMultiFileModel for VLMs
Use registerMultiFileModel for VLMs
VLM models always require two GGUF files — the language model and the multimodal projector (
mmproj). Use registerMultiFileModel instead of registerModel. Missing the projector file will cause loading failures.Retrieve ModelDescriptor before loading
Retrieve ModelDescriptor before loading
Unlike LLM loading which accepts a plain string ID, VLM loading requires a full
ModelDescriptor
from availableModels(). Always fetch and filter the descriptor before calling loadVLMModel.Handle platform differences
Handle platform differences
Use
#if os(iOS) and #elseif os(macOS) for VLMImage construction. iOS uses UIImage directly
while macOS requires raw RGB pixel data with explicit dimensions.Limit maxTokens for quick descriptions
Limit maxTokens for quick descriptions
Use 30–64 tokens for one-line descriptions and 128–256 for detailed analysis. Larger values
increase latency on mobile devices.
Cancel long generations
Cancel long generations
Always provide a cancel button in your UI. Call
cancelVLMGeneration() to immediately halt token
generation and free resources.Downscale images before processing
Downscale images before processing
Large images increase encoding time significantly. Resize to 256–512px on the longest edge before creating a
VLMImage for optimal latency.Supported Models
Related
LLM Generation
Text-only generation
Tool Calling
Function calling with LLMs
Image Generation
Generate images with Stable Diffusion
Best Practices
Performance optimization