All functions listed in this document are safe to call from the main thread and all callbacks will be run on the main thread, unless there are explicit instructions or explanations.
ModelRunner
A ModelRunner represents a loaded model instance. The SDK returns concrete ModelRunner implementations, but your code only needs the protocol surface:
Lifecycle
- Create conversations using
createConversation(systemPrompt:)orcreateConversationFromHistory(history:). - Hold a strong reference to the
ModelRunnerfor as long as you need to perform generations. - Call
unload()when you are done to release native resources (optional, happens automatically on deinit). - Access
modelIdto identify the loaded model (for analytics, debugging, or UI labels).
Low-level generation API
generateResponse(...) drives generation with callbacks and returns a GenerationHandler you can store to cancel the run. Most apps call the higher-level streaming helpers on Conversation, but you can invoke this method directly when you need fine-grained control (for example, integrating with custom async primitives).
GenerationHandler
ModelRunner.generateResponse or Conversation.generateResponse(..., onResponse:) lets you cancel generation without tearing down the conversation.
Conversation
Conversation tracks chat state and provides streaming helpers built on top of the model runner.
Properties
history: Copy of the accumulated chat messages. The SDK appends the assistant reply when a generation finishes successfully.functions: Functions registered viaregisterFunction(_:)for function calling.isGenerating: Boolean flag indicating whether a generation is currently running. Attempts to start a new generation while this istrueimmediately finish with an empty stream (ornilhandler for the callback variant).
Streaming Convenience
The most common pattern is to use the async-stream helpers:Callback Convenience
UsegenerateResponse(message:onResponse:) when you prefer callbacks or need to integrate with imperative UI components:
nil and emits a .complete message with finishReason == .stop via the callback.
Export Chat History
exportToJSON() serializes the conversation history into a [[String: Any]] payload that mirrors OpenAIβs chat-completions format. This is useful for persistence, analytics, or debugging tools.
MessageResponse
chunk: Partial assistant text emitted during streaming.reasoningChunk: Model reasoning tokens wrapped between<think>/</think>(only for models that expose reasoning traces).audioSample: PCM audio frames streamed from audio-capable checkpoints. Feed them into an audio renderer or buffer for later playback.functionCall: One or more function/tool invocations requested by the model. See the Function Calling guide.complete: Signals the end of generation. Access the assembled assistant reply throughcompletion.message. Stats and finish reason live on thecompletionobject;completion.infois provided for backward compatibility.
AsyncThrowingStream, or via the onErrorCallback closure when using the lower-level API.
GenerationOptions
Tune generation behavior with GenerationOptions.
- Leave a field as
nilto fall back to the defaults packaged with the model bundle. functionCallParsercontrols how tool-call tokens are parsed.LFMFunctionCallParser(the default) handles Liquid Foundation Model Pythonic function calling. SupplyHermesFunctionCallParser()for Hermes/Qwen3 formats, or set the parser tonilto receive raw tool-call text inMessageResponse.chunk.jsonSchemaConstraintactivates constrained generation. UsesetResponseFormat(type:)to populate it from a type annotated with the@Generatablemacro.
LiquidInferenceEngineRunner exposes advanced utilities such as getPromptTokensSize(messages:addBosToken:) for applications that need to budget tokens ahead of time. These methods are backend-specific and may be elevated to the ModelRunner protocol in a future release.