LiteRT Programming Languages: Complete Guide to Multi-Language Inference
LiteRT officially supports C, C++, Python, JavaScript/WebAssembly, Kotlin, Java, and Swift, providing stable APIs for cross-platform on-device machine learning inference.
LiteRT (Lite Runtime) is a multi-language, cross-platform inference engine developed by Google AI Edge. The google-ai-edge/LiteRT repository ships official language bindings that enable developers to deploy machine learning models across mobile, web, and embedded environments using their preferred LiteRT programming languages.
Official LiteRT Language Bindings
The repository provides first-class support for seven programming languages through distinct API layers:
- C – Stable ABI-compatible API for runtime, model building, and profiling
- C++ – Public, non-ABI-stable API with convenient wrappers and RAII semantics
- Python – CPython extension exposing C and C++ APIs via
pybind11 - JavaScript / WebAssembly – Wasm-based runtime for browsers and Node.js
- Kotlin – Android-focused wrapper forwarding calls to the C API via JNI
- Java – Android Java API built on top of the C runtime
- Swift – iOS/macOS Swift package wrapping the C/C++ APIs
Each binding targets specific deployment environments while sharing the same underlying inference engine.
Language-Specific APIs and Implementation Details
C API (Stable ABI)
The C API provides the foundational, ABI-stable interface defined in litert/c/litert_model.h. This layer offers the smallest binary footprint and serves as the canonical runtime interface for embedded and cross-language scenarios.
Key functions include LiteRtCreateModelFromBuffer for loading models, LiteRtCreateRuntimeContext for initialization, and LiteRtCompiledModelExecute for inference.
#include "litert/c/litert_model.h"
int main() {
// Load the model binary.
const char *buf = /* read model.tflite into memory */;
size_t size = /* size of the buffer */;
LiteRtModel model = NULL;
LiteRtStatus status = LiteRtCreateModelFromBuffer(buf, size, &model);
if (status != kLiteRtStatusOk) return -1;
// Create a runtime context.
LiteRtRuntimeContext ctx = NULL;
status = LiteRtCreateRuntimeContext(&ctx);
if (status != kLiteRtStatusOk) return -1;
// Build a compiled model for the default CPU.
LiteRtCompiledModel compiled = NULL;
LiteRtCompilerPlugin compiler = NULL;
LiteRtCreateCompilerPlugin(&compiler);
status = LiteRtCompilerPluginCompile(compiler,
model,
/*options=*/NULL,
&compiled);
if (status != kLiteRtStatusOk) return -1;
// Execute the model (simplified).
LiteRtExecutionContext exec_ctx = NULL;
LiteRtCreateExecutionContext(&exec_ctx);
LiteRtExecutionContextSetInput(exec_ctx, 0, /*input tensor*/);
LiteRtCompiledModelExecute(compiled, exec_ctx);
// Retrieve output, clean up …
}
C++ API (Public Wrappers)
The C++ API in litert/cc/litert_model.h provides object-oriented wrappers with RAII semantics. While not ABI-stable, it offers a more convenient interface for native development.
The litert::Model class provides CreateFromFile for loading, while litert::CompilerPlugin handles model compilation.
#include "litert/cc/litert_model.h"
int main() {
// Load model from file.
auto model = litert::Model::CreateFromFile("model.tflite");
if (!model) return -1;
// Build a compiled model for CPU.
auto compiler = litert::CompilerPlugin::Create();
auto compiled = compiler->Compile(*model);
// Create an execution context.
auto exec = compiled->CreateExecutionContext();
exec->SetInput(/*tensor data*/);
exec->Invoke();
// Get output tensor.
auto output = exec->GetOutput(/*index*/);
}
Python API
The Python bindings are implemented in litert/python/converter_wrapper.cc using pybind11 to expose the full C/C++ API. This enables rapid prototyping while maintaining the performance of the underlying engine.
import litert
# Load model.
model = litert.load_model("model.tflite")
# Compile for the default device (CPU).
compiled = model.compile()
# Create an execution context.
exec_ctx = compiled.create_execution_context()
exec_ctx.set_input(0, input_tensor) # NumPy array, for example
exec_ctx.invoke()
# Retrieve result.
output = exec_ctx.get_output(0)
print(output)
JavaScript and WebAssembly
The JavaScript API loads a WebAssembly-based runtime defined in litert/js/packages/core/src/load_litert.ts. This enables model execution in browsers and Node.js environments without requiring native installation.
import { loadLiteRt } from '@litert/js';
// Load the runtime (Wasm) and the model binary.
async function run() {
const litert = await loadLiteRt('litert_wasm_internal.js');
const modelBytes = await fetch('model.tflite').then(r => r.arrayBuffer());
const model = litert.createModelFromBuffer(new Uint8Array(modelBytes));
const compiled = model.compile(); // CPU by default
const exec = compiled.createExecutionContext();
exec.setInput(0, new Float32Array([/* … */]));
await exec.invoke();
const output = exec.getOutput(0);
console.log(output);
}
run();
Kotlin (Android)
The Kotlin API in litert/kotlin/ provides Android-focused wrappers that forward calls to the C API via JNI. These classes are generated from the Java binding but offer idiomatic Kotlin syntax.
import com.google.litert.LiteRtModel
import com.google.litert.LiteRtCompiledModel
// Load model from assets.
val assetManager = context.assets
val model = LiteRtModel.createFromAsset(assetManager, "model.tflite")
// Compile for default CPU.
val compiled: LiteRtCompiledModel = model.compile()
// Create execution context.
val exec = compiled.createExecutionContext()
exec.setInput(0, inputTensor) // Tensor defined by LiteRtTensor
exec.invoke()
val output = exec.getOutput(0)
Java (Android)
The Java API is defined in tflite/java/src/main/java/org/tensorflow/lite/Interpreter.java. It provides a high-level Android interface built on top of the C runtime.
import org.tensorflow.lite.Interpreter;
// Load the model.
Interpreter interpreter = new Interpreter(
getAssets().openFd("model.tflite"));
// Prepare input and output buffers.
float[][] input = new float[1][224 * 224 * 3];
float[][] output = new float[1][1001];
// Run inference.
interpreter.run(input, output);
Swift (iOS/macOS)
The Swift package is located in tflite/swift/Sources/Interpreter.swift. It wraps the C/C++ APIs for iOS and macOS development.
import TensorFlowLite
// Load model.
let interpreter = try Interpreter(modelPath: "model.tflite")
// Allocate tensors.
try interpreter.allocateTensors()
// Set input data.
var inputData = Data(...)
try interpreter.copy(inputData, toInputAt: 0)
// Run inference.
try interpreter.invoke()
// Get output.
let outputTensor = try interpreter.output(at: 0)
let results = [Float](unsafeData: outputTensor.data) // Convert to Float array
print(results)
Key Source Files and Entry Points
The following files define the primary entry points for each LiteRT programming language binding:
- C:
litert/c/litert_model.h– Stable ABI for model loading, compilation, and execution. - C++:
litert/cc/litert_model.h– Object-oriented wrappers with RAII semantics. - Python:
litert/python/converter_wrapper.cc–pybind11extension exposing the C/C++ API. - JavaScript / WebAssembly:
litert/js/packages/core/src/load_litert.ts– Wasm runtime loader for browsers and Node.js. - Kotlin:
litert/kotlin/– Android-specific Kotlin wrappers generated from Java bindings. - Java:
tflite/java/src/main/java/org/tensorflow/lite/Interpreter.java– High-level Android Java API. - Swift:
tflite/swift/Sources/Interpreter.swift– Swift package for iOS and macOS.
These implementations collectively make LiteRT a polyglot runtime, enabling integration into native C/C++ projects, rapid prototyping in Python, web deployment via JavaScript/Wasm, and mobile embedding using Kotlin/Java (Android) or Swift (iOS).
Summary
- LiteRT provides official language bindings for C, C++, Python, JavaScript/WebAssembly, Kotlin, Java, and Swift.
- The C API (
litert/c/litert_model.h) provides the stable ABI foundation used by all other language bindings. - C++ wrappers (
litert/cc/) offer RAII semantics and object-oriented convenience while calling the C core. - Python bindings use
pybind11to expose the full C/C++ API for rapid prototyping. - JavaScript/WebAssembly support enables browser-based inference via
litert/js/packages/core/src/load_litert.ts. - Kotlin and Java APIs target Android development, with Kotlin providing modern Android syntax.
- Swift support delivers first-class iOS and macOS integration through the
tflite/swift/package.
Frequently Asked Questions
What programming languages does LiteRT support?
LiteRT officially supports seven programming languages: C, C++, Python, JavaScript (via WebAssembly), Kotlin, Java, and Swift. These bindings enable deployment across embedded systems, desktop applications, web browsers, and mobile platforms including Android and iOS.
Does LiteRT support WebAssembly for browser-based inference?
Yes, LiteRT provides a WebAssembly-based runtime accessible through JavaScript. The litert/js/packages/core/src/load_litert.ts module loads the Wasm binary, allowing models to execute directly in browsers and Node.js without native installation or platform-specific binaries.
Which LiteRT API should I use for Android development?
For Android development, use the Kotlin API (litert/kotlin/) for modern Android applications, or the Java API (tflite/java/src/main/java/org/tensorflow/lite/Interpreter.java) for legacy support. Both APIs delegate to the underlying C runtime via JNI and provide high-level abstractions for model loading and inference.
Is the LiteRT C API stable for long-term production use?
Yes, the C API defined in litert/c/litert_model.h is designed as a stable, ABI-compatible interface. It serves as the canonical runtime foundation for all other language bindings and is intended for production use in embedded and cross-platform scenarios requiring long-term binary stability.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →