ML Kit Genai Prompt API
Android (Google)OfficialCreated by Google LLCDistributed by Android (Google)
- Skill
- Tool integration
- Software development
- Apache-2.0
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a comprehensive guide for developers to implement the ML Kit GenAI Prompt API within Android projects. It covers the end-to-end lifecycle of on-device generative AI, including model availability checks, dependency configuration, and resource management. The skill also details advanced optimization techniques such as prefix caching for reduced latency and structured output generation for type-safe data handling.
What it does
- Configures Android projects with the necessary ML Kit GenAI dependencies.
- Provides patterns for checking model availability and managing the download lifecycle.
- Demonstrates how to implement implicit and explicit prefix caching to improve inference speed.
- Guides the refactoring of string-based prompts into type-safe structured output using annotations.
- Offers best practices for resource management, including proper model instance disposal.
- Provides optimization strategies for prompt design to enhance model performance.
When to use it
- When integrating on-device generative AI features using Gemini Nano in Android apps.
- When you need to optimize inference latency for recurring prompt prefixes.
- When you need to ensure your AI responses are returned in a structured, type-safe format.
- When managing the lifecycle of generative model instances to prevent memory leaks.
Advantages
- Reduces inference latency through efficient prefix caching mechanisms.
- Enables type-safe AI interactions by mapping responses directly to data classes.
- Provides clear lifecycle management patterns for stable app performance.
- Offers granular control over model state and cache management.
Usage examples
- “How do I check if Gemini Nano is ready to use on the device?”
- The skill provides the logic to check the FeatureStatus of the generative model and handle the download process if the model is not yet available.
- “How can I refactor my string-based prompt to return a structured data object?”
- The skill demonstrates using the @Generable annotation and the generateTypedContentRequest method to map AI responses to a specific Kotlin data class.
- “How do I speed up my app's AI response time for long prompts?”
- The skill explains how to implement prefix caching by separating static prompt prefixes from dynamic suffixes.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/android/ml-kit-genai-prompt-api/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip ml-kit-genai-prompt-api.zip -d .agents/skills
Documentation by Google LLC
This skill provides step-by-step guidance for integrating and optimizing the ML Kit GenAI Prompt API in Android apps.
Prerequisites
Android API level must be 26 or higher. If
minSdkis below 26, update it to 26.Add the ML Kit GenAI Prompt API dependency (
com.google.mlkit:genai-prompt) to the app-levelbuild.gradlefile, with version at least1.0.0-beta4.If
com.google.mlkit:genai-schema-compilerdependency is used and KSP plugin version is below 2.3.6, update it to 2.3.6.
Detailed steps
1. Prompt optimization
To optimize prompts for use with the ML Kit Prompt API, follow the prompt optimization guide.
2. Prefix caching optimization
If the prompt is more than 200 words, implement the prefix caching API.
3. Lifecycle and best practices
The model must be fully downloaded and available before calling the first inference. Follow the guide on implementing a generative model to check that the
FeatureStatusof a model isAVAILABLEbefore making an inference.Release ML Kit instances by calling
close()when anActivity,Fragment, orViewModelis destroyed. Example:// Instantiating model in activity, fragment, or ViewModel val generativeModel = Generation.getClient() // When activity, fragment, or ViewModel is destroyed generativeModel.close()<br />
4. Structured output
When implementing or refactoring a prompt to use structured output, follow these rules:
Check for API availability: Verify Structured Output feature is available on the device with
isStructuredOutputFeatureAvailable()before using it. Refer to the Structured Output API guide for full instructions.Return type: Return the
@Generabletyped object from the function signature instead of aStringor JSON string.For example:
fun parseEmail(email: String): String { ... }should be refactored to:
fun parseEmail(email: String): ParsedEmail? { ... }Example:
This is the example code before refactoring:
suspend fun parseEmail(email: String): String { val parseEmailPrompt = "Parse this email and return the sender, title, and short summary of the email less than 10 words: " val parsedEmail = generativeModel.generateContent(parseEmailPrompt + email) return parsedEmail.candidates[0].text }<br />
This is the example code after using Structured Output API:
@Generable data class ParsedEmail( @Guide(description = "Sender of the email") var sender: String = "", @Guide(description = "Title of the email") var title: String = "", @Guide(description = "Summary of the email less than 10 words") var summary: String = "" ) suspend fun parseEmail(email: String): ParsedEmail? { val parseEmailPrompt = "Parse this email: $email" val baseRequest = GenerateContentRequest.Builder(TextPart(parseEmailPrompt)).build() val typedRequest = generateTypedContentRequest(baseRequest, ParsedEmail::class) val typedResponse = generativeModel.generateContent(typedRequest) return typedResponse.candidates[0].response }<br />
Files
- ATTRIBUTION.mdAdded by AutoGineer986 B
- LICENSELicence file added by AutoGineer11.1 KiB
- SKILL.md4.1 KiB
- references/get-started.md10.6 KiB
- references/prefix-caching.md5.7 KiB
- references/prompt-design.md3.5 KiB
- references/prompt-optimization.md4.5 KiB
- references/structured-output.md10.2 KiB
- references/system-instructions.md3.3 KiB
references/prompt-optimization.md
This skill provides guidance on how to optimize prompts for use with the ML Kit Prompt API.
Guidelines & Rules
The best practices for prompt design for Prompt API are at Prompt Design. Apply these core rules:
Include examples for in-context learning with examples of the output. This is an example prompt that includes in-context learning because it has examples of the desired model output: Analyze the message and return whether the customer sentiment is positive, negative, or neutral. Example 1: Input - "This product doesn't work"; Output - "Negative" Example 2: Input - "I liked this product"; Output - "Positive"
Make prompts concise. Remove duplicate or repeated instructions. Do not include conversational filler such as: hello, please, do this, help me with, etc. For example, a verbose prompt such as: "Hello! I want you to act as an expert translator for me today. I am going to give you a short paragraph of text in English, and I would really like it if you could translate it into formal, high-level Business French. Please make sure that you do not include any intro like 'Here is your translation' or any closing remarks at all. Just output the translation itself." can be improved with this concise prompt: "Translate to formal Business French. Return ONLY the translation"
Use paired XML/HTML delimiters to denote dynamic inputs (for example
<email>[content]</email>) to clearly isolate user data from prompt instructions. Mention that the delimiters represent placeholder text that should be replaced by code in the actual implementation.Use the Structured Output API if the output requires parsing responses into certain formats. Don't include the Structured Output API implementation in the prompt itself, but mention it after presenting the optimized prompt to the user so the user knows how to parse the model output. Prompts that use Structured Output API must return the
@Generabletyped object from the function signature instead of a string or JSON string.Keep output short by adding output constraints such as word count, character count, or number of bullets or sentences. For example, add constraints such as "The summarization must be 10 words or fewer."
Use system instructions for short instructions that define how a model should behave, and use prefix caching for prompts that are over 200 words.
Examples
Here are examples of prompts that the user might ask you to optimize, and the improved, optimized versions.
Example 1
Unoptimized prompt:
Process this customer email and return the order ID, what was bought, the phone
number, and what they want. Format it as JSON so my app can read it.Optimized prompt:
## Task
Extract key details from the customer email below.
## Customer Email
<email>
[email_content]
</email>Explanation: Because the information that the user wants to extract using the prompt is well suited for the Structured Output API, the optimized prompt implicitly uses the Structured Output API and doesn't need to explicitly define the schema for the extracted data. In your response to the user that explains the optimizations made to the prompt, mention that the optimized prompt is designed for use with the Structured Output API. Also, mention that the "" delimiters signify placeholder text that should be replaced with code in the user's actual implementation.
Example 2
Unoptimized prompt:
Given this itinerary for a trip: [Flight to Paris (CDG), Check-in: Hotel Le
Meurice, Visit to the Louvre, Dinner at Le Jules Verne, Eiffel Tower Visit,
Versailles Palace Tour, Montmartre Walk, Seine River Cruise, Pastry and Macaron
Tasting, Flight Out], generate the following: overall vibe, tips on how to
prepare for this trip, and common short phrases to learn for the trip.Optimized prompt:
## Task
Analyze the trip itinerary below and extract details to build a concise travel
guide.
## Rules
- Overall vibe: Limit to 1 short sentence (under 15 words).
- Preparation tips: Provide exactly 3 short tips (maximum 10 words per tip).
- Useful phrases: Provide exactly 3 phrases (under 15 words each).
## Itinerary
<itinerary>
[itinerary_content]
</itinerary>Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Google LLC
- Publisher
- Android (Google) (Official)
- Distributed by
- Android (Google)
- Source
- android/skills · device-ai/ml-kit-genai-prompt-api · commit bf68e2d
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE.txt)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Google LLC or Android (Google).
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download ml-kit-genai-prompt-api.zip