View Source Code
Browse the complete example on GitHub
What’s inside?
The RecipeGenerator demonstrates advanced LeapSDK capabilities:- Structured Output Generation - Enforce JSON schema constraints on model outputs
- Automatic Model Downloading - Models download automatically via
LeapDownloaderon first run - Constrained Decoding - Guide the model to produce valid, parseable data structures
- Schema Validation - Ensure generated recipes follow a specific format
- Type Safety - Parse AI outputs directly into Kotlin data classes with kotlinx.serialization
- @Generatable Annotation - Use LeapSDK’s annotation for simplified structured output generation
- Production-ready Pattern - Integrate AI outputs with databases and business logic
- Practical Use Case - Generate recipes with ingredients, steps, and metadata
What is constrained generation?
Constrained generation refers to the ability to enforce specific formatting or structural requirements on model outputs. Instead of generating free-form text, the model produces outputs that conform to a predefined schema or pattern. Benefits:- Reliability - Guaranteed parseable output every time
- Type Safety - Direct integration with typed programming languages
- Validation - Automatic schema compliance
- Downstream Integration - Feed structured data into databases, APIs, or UIs
- Error Reduction - Eliminate parsing errors from inconsistent formats
- JSON API responses
- Database records
- Form submissions
- Product catalogs
- Configuration files
- Structured reports
Learn More About Constrained Generation
Read the complete guide in the official Leap documentation
Environment setup
Before running this example, ensure you have the following:Android Studio Installation
Android Studio Installation
Download and install Android Studio (latest stable version recommended).Make sure you have:
- Android SDK installed
- An Android device or emulator configured
- USB debugging enabled (for physical devices)
Minimum SDK Requirements
Minimum SDK Requirements
This example requires:
- Minimum SDK: API 24 (Android 7.0)
- Target SDK: API 34 or higher
- Kotlin: 1.9.0 or higher
- LeapSDK: 0.9.4 or higher
- Internet connectivity: Required for first-time model download
Model Setup - Automatic Download
Model Setup - Automatic Download
This example uses LeapSDK 0.9.4+ with automatic model downloading capabilities.Automatic Model ManagementThe app uses
LeapDownloader to automatically download and cache the LFM2-700M model on first run:- On first launch, the model downloads automatically from Leap Model Library
- Models are cached locally for subsequent app launches
- No manual ADB push or file transfer required
- Internet connectivity is required only for the initial download
- Launch the app (internet connection required)
- The model downloads automatically (may take 2-5 minutes depending on connection)
- Once cached, subsequent launches work offline
- The model persists across app restarts
Dependencies Setup
Dependencies Setup
Add the required dependencies to your app-level Also enable the Kotlin serialization plugin in your app-level
build.gradle.kts:build.gradle.kts:How to run it
Follow these steps to generate structured recipes:-
Clone the repository
-
Open in Android Studio
- Launch Android Studio
- Select “Open an existing project”
- Navigate to the
RecipeGeneratorfolder and open it
-
Gradle sync
- Wait for Gradle to sync all dependencies
- Ensure LeapSDK 0.9.7 is downloaded
-
Run the app
- Connect your Android device or start an emulator
- Ensure internet connectivity (required for first-time model download)
- Click “Run” or press
Shift + F10 - Select your target device
-
First launch - model download
- On first run, the app will automatically download the LFM2-700M model
- This may take 2-5 minutes depending on your connection
- A loading indicator will show download progress
- The model is cached for future use
-
Generate recipes
- Enter ingredients or a dish name (e.g., “pasta carbonara”, “chocolate chip cookies”)
- Tap “Generate Recipe”
- The app will produce a structured recipe with ingredients, steps, and metadata
- All output will be valid JSON conforming to the recipe schema
After First Run: The model is cached locally. Subsequent app launches work offline and start immediately without downloading.
Code walkthrough
The core business logic is implemented inMainActivityViewModel.kt. Here’s how it works:
Define the Recipe Schema
First, define the data structure you want the AI to generate:Configure Constrained Generation
Set up the LeapSDK to enforce the schema:Generate Structured Recipes
Create a function to generate recipes with guaranteed structure:Alternative: Using @Generatable Annotation
LeapSDK 0.9.4+ provides the@Generatable annotation for simplified structured output:
@Generatable annotation automatically creates the JSON schema from your Kotlin data class, eliminating manual schema definition.
Display the Recipe
The structured output can be easily rendered in the UI:Results
The RecipeGenerator produces consistently formatted recipes: Example Output:
The interface shows the structured recipe data rendered beautifully with proper formatting, making it easy to read and follow.
Further improvements
Here are some ways to extend this example:- Database integration - Save generated recipes to Room database
- Multiple cuisines - Add cuisine type selector (Italian, Mexican, Asian, etc.)
- Dietary restrictions - Filter for vegan, gluten-free, keto, etc.
- Nutritional information - Extend schema to include calories, protein, carbs
- Scaling calculator - Adjust ingredient amounts based on servings
- Shopping list generation - Extract ingredients into a shareable shopping list
- Image generation - Integrate with image models to visualize the dish
- Recipe sharing - Export as PDF or share via social media
- User ratings - Allow users to rate and review generated recipes
- Variation generator - Generate recipe variations (e.g., “make it vegan”)
- Meal planning - Combine multiple recipes into weekly meal plans
- Ingredient substitution - Suggest alternatives for missing ingredients