flutter_edge_ai_mediapipe 1.2.6
flutter_edge_ai_mediapipe: ^1.2.6 copied to clipboard
MediaPipe (.task) on-device inference engine for flutter_edge_ai (Android/iOS/Web). Opt-in package; provides an InferenceEngineProvider. Bundles Google's MediaPipe Pod/Gradle deps.
flutter_edge_ai_mediapipe #
Renamed from
flutter_gemma_mediapipe. Same package, new name: swap the dependency and thepackage:flutter_gemma_mediapipe/imports; nothing on the device changes. See the migration guide.
MediaPipe (.task) on-device inference engine for flutter_edge_ai. Opt-in package — add it only if you run MediaPipe .task models. Android, iOS, and Web.
Google moved MediaPipe LLM Inference to maintenance-only. New features and optimizations focus on LiteRT-LM, so a new app should use
.litertlmmodels withflutter_edge_ai_litertlm. On iOS, do not load a.taskmodel on the GPU: Gemma 3 1B reached the app's memory limit while loading and iOS terminated the app (details).
Teach your AI assistant this package #
dart run skills@ get --all
Installs the agent skills flutter_edge_ai bundles — this package depends on it, so they come with it. One of them, flutter-edge-ai-mediapipe, covers .task and .bin models on Android, iOS and web.
Usage #
import 'package:flutter_edge_ai/flutter_edge_ai.dart';
import 'package:flutter_edge_ai_mediapipe/flutter_edge_ai_mediapipe.dart';
await FlutterEdgeAi.initialize(
inferenceEngines: [MediaPipeEngine()],
);
MediaPipeEngine handles ModelFileType.task / ModelFileType.binary (.bin, .tflite) models; pass it alongside other engines (e.g. LiteRtLmEngine from flutter_edge_ai_litertlm) if your app uses both formats.
maxTokens is the whole context (input plus output), and a small value is not raised as it is on .litertlm. maxOutputTokens is ignored (only a debug log says so); stop early with session.stopGeneration().
On Android and iOS, pass supportImage: true to createChat as well as to getActiveModel, or the image is dropped. The Gemma 3n .task files carry no audio encoder; use a .litertlm model for audio.
Sampling #
A .task file ships no sampler, so an unset temperature, topK or topP comes from the model family's defaults (SamplingParams.forModelType), with an unset topK capped at 40, MediaPipe's own default; an unset randomSeed is 1. topK: 1 or temperature: 0 decodes greedily, as Phi and Hammer do by default; before 1.2.0 every family did. On Android and iOS each session starts from its seed, so vary randomSeed for a different answer to the same prompt. On web the loaded model keeps the sampling of its first session, so the sampling of a later createSession is not applied; web also has no top-p, and a topP you pass is ignored with a one-time warning.
Web setup #
On Web, the MediaPipe runtime is loaded from a CDN. Add this to your app's web/index.html (inside a <script type="module"> before your Flutter bootstrap), exposing the symbols on window:
<script type="module">
import { FilesetResolver, LlmInference } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai@0.10.30';
window.FilesetResolver = FilesetResolver;
window.LlmInference = LlmInference;
</script>
Load the model's -web.task build (e.g. gemma3-1b-it-int4-web.task); mobile .task files generally do not run on web. Also copy cache_api.js and opfs_helper.js from the flutter_edge_ai package's web/ directory into your app's web/ and load them inside the <head> of web/index.html with <script src="cache_api.js"></script> and <script src="opfs_helper.js"></script> — see Installation → Web. Web runs on the GPU only.
On web, MediaPipe keeps nothing between calls, so the session keeps the conversation itself and sends all of it with every turn: each answer re-reads the whole history, and its time grows with the chat. Past turns stay in it with the model's replies, images and audio included, so maxNumImages counts every image of the conversation, not one prompt. A turn that would go past it fails with an error that names maxNumImages; nothing is dropped. Each createSession (or createChat) starts a new conversation and closes the previous session. The turn markers are read from the .task file's own metadata (Gemma 4 declares <|turn>), and a file that declares none falls back to its model type. A .bin file and FunctionGemma keep the markers core writes into their messages, and a .task that carries MediaPipe's own prompt_templates is sent without markers of ours. stopGeneration() keeps what a streamed reply had produced; on a call that is not streaming nothing has been produced, so the next user message joins the unanswered turn. A generation that fails also leaves its user turn in the conversation; to retry it, call getResponse() (or getResponseAsync()) again without adding the message a second time, or the turn holds it twice.
(Android needs no extra setup — the MediaPipe Gradle deps are bundled by this package.)
iOS setup #
iOS needs a 16.0 minimum. MediaPipe GenAI requires it — core, flutter_edge_ai_litertlm,
built-in AI and embeddings build from 15.0 (#441); flutter_edge_ai_llama needs 16.4.
This package ships no Package.swift, so even an app on Swift Package Manager gets a
Podfile for it. Set the floor in BOTH places: platform :ios, '16.0' in the Podfile,
and iOS Deployment Target on the Runner target in Xcode. Below 16, pod install
reports that specs satisfying the flutter_edge_ai_mediapipe dependency "required a higher
minimum deployment target".
MediaPipe ships static xcframeworks, so the Podfile also needs
use_frameworks! :linkage => :static — with the Flutter template's bare
use_frameworks!, pod install fails on "transitive dependencies that include
statically linked binaries".
Platforms #
| Platform | Support |
|---|---|
| Android | ✅ com.google.mediapipe:tasks-genai 0.10.35 (Gradle) |
| iOS | ✅ MediaPipeTasksGenAI 0.10.35 (CocoaPods) — requires iOS 16.0+; run .task models on PreferredBackend.cpu |
| Web | ✅ @mediapipe/tasks-genai 0.10.30 (CDN, see above) — GPU only, -web.task models |
| Desktop | ❌ (MediaPipe .task not supported on desktop — use flutter_edge_ai_litertlm) |