The latest Gemini models, likeGemini 3.1 Flash Image(Nano Banana 2), are available to use with Firebase AI Logic on all platforms!Learn more.
Gemini 2.0 Flash and Flash-Lite models will be retired onJune 1, 2026. To avoid service disruption, update to a newer model likegemini-2.5-flash-lite.Learn more.
Build hybrid experiences in Web apps with on-device and cloud-hosted modelsStay organized with collectionsSave and categorize content based on your preferences.
Build AI-powered web apps and features with hybrid inference usingFirebase AI Logic. Hybrid inference enables running inference using
on-device models when available and seamlessly falling back to cloud-hosted
models otherwise (and vice versa).
Reach 100% of your audience, regardless of on-device model availability
or internet connectivity
Supported capabilities and features for on-device inference:
On-device inference only supportssingle-turn text generation (notchat),
with streaming or non-streaming output. It supports the following
text-generation capabilities:
Inference using an on-device model uses thePrompt API from Chrome;
whereas inference using a cloud-hosted model uses your chosenGemini APIprovider (either theGemini Developer APIor theVertex AIGemini API).
This page describes how toget started developing using localhost(learn
more aboutusing APIs on localhostin the Chrome documentation).
(Optional)Download the on-device model before the first request.
The Prompt API is built into Chrome; however, the on-device model isn't
available by default. If you haven't yet downloaded the model before your
first request for on-device inference, the request will automatically start
the model download in the background.
View instructions to download the on-device model
OpenDeveloper Tools > Console.
Run the following:
awaitLanguageModel.availability();
Make sure that the output isavailable,downloading, ordownloadable.
If the output isdownloadable, start the model download by running:
awaitLanguageModel.create();
You can use the followingmonitorcallback to listen for download
progress and make sure that the model isavailablebefore making
requests:
Step 2: Set up a Firebase project and connect your app to Firebase
Sign into theFirebaseconsole,
and then select your Firebase project.
Don't already have a Firebase project?
If you don't already have a Firebase project, click the button to create a
new Firebase project, and then use either of the following options:
Option 1: Create a wholly new Firebase project (and its underlyingGoogle Cloudproject automatically) by entering a new project name in the
first step of the workflow.
Option 2: "Add Firebase" to an existingGoogle Cloudproject by
clickingAdd Firebase to Google Cloud project(at bottom of page).
In the first step of the workflow, start entering theproject nameof
the existing project, and then select the project from the displayed list.
Complete the remaining steps of the on-screen workflow to create a Firebase
project. Note that when prompted, you donotneed to set upGoogle Analyticsto use theFirebase AI Logic SDKs.
ClickGet startedto launch a guided workflow that helps you set up therequired APIsand resources for your project.
Set up your project to use a "Gemini API" provider.
We recommend getting started using theGemini Developer API.At any point, you can alwaysset up theVertex AIGemini API(and its requirement for billing).
For theGemini Developer API, the console will enable the required
APIs and create aGeminiAPI key in your project. Donotadd thisGeminiAPI key into your app's codebase.Learn more.
If prompted in the console's workflow, follow the on-screen instructions to
register your app and connect it to Firebase.
Continue to the next step in this guide to add the SDK to your app.
Step 3: Add the SDK
The Firebase library provides access to the APIs for interacting with generative
models. The library is included as part of the Firebase JavaScript SDK for Web.
Install the Firebase JS SDK for Web using npm:
npm install firebase
Initialize Firebase in your app:
import{initializeApp}from"firebase/app";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);
Step 4: Initialize the service and create a model instance
Click yourGemini APIprovider to view provider-specific content
and code on this page.
Set up the following before you send a prompt request to the model.
Initialize the service for your chosen API provider.
Create aGenerativeModelinstance. Make sure to do the following:
CallgetGenerativeModelafter or on an end-user interaction(like a
button click). This is a prerequisite forinferenceMode.
Set themodeto one of:
PREFER_ON_DEVICE: Use the on-device model if it's available;
otherwise,fall back to the cloud-hosted model.
ONLY_ON_DEVICE: Use the on-device model if it's available;
otherwise,throw an exception.
PREFER_IN_CLOUD: Use the cloud-hosted model if it's available;
otherwise,fall back to the on-device model.
ONLY_IN_CLOUD: Use the cloud-hosted model if it's available;
otherwise,throw an exception.
import{initializeApp}from"firebase/app";import{getAI,getGenerativeModel,GoogleAIBackend,InferenceMode}from"firebase/ai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Gemini Developer API backend serviceconstai=getAI(firebaseApp,{backend:newGoogleAIBackend()});// Create a `GenerativeModel` instance// Call `getGenerativeModel` after or on an end-user interaction// Set the mode (for example, use the on-device model if it's available)constmodel=getGenerativeModel(ai,{mode:InferenceMode.PREFER_ON_DEVICE});
Step 5: Send a prompt request to a model
This section shows you how to send various types of input to generate different
types of output, including:
Before trying this sample, make sure that you've completed theGet startedsection of this guide.
You can usegenerateContent()to generate text from a prompt that contains text:
// Imports + initialization of FirebaseApp and backend service + creation of model instance// Wrap in an async function so you can use awaitasyncfunctionrun(){// Provide a prompt that contains textconstprompt="Write a story about a magic backpack."// To generate text output, call `generateContent` with the text inputconstresult=awaitmodel.generateContent(prompt);constresponse=result.response;consttext=response.text();console.log(text);}run();
Note thatFirebase AI Logicalso supports streaming of text responses usinggenerateContentStream(instead ofgenerateContent).
Generate text from text-and-image (multimodal) input
Before trying this sample, make sure that you've completed theGet startedsection of this guide.
You can usegenerateContent()to generate text from a prompt that contains text and image files—providing each
input file'smimeTypeand the file itself.
The supported input image types for on-device inference are PNG and JPEG.
// Imports + initialization of FirebaseApp and backend service + creation of model instance// Converts a File object to a Part object.asyncfunctionfileToGenerativePart(file){constbase64EncodedDataPromise=newPromise((resolve)=>{constreader=newFileReader();reader.onloadend=()=>resolve(reader.result.split(',')[1]);reader.readAsDataURL(file);});return{inlineData:{data:awaitbase64EncodedDataPromise,mimeType:file.type},};}asyncfunctionrun(){// Provide a text prompt to include with the imageconstprompt="Write a poem about this picture:";constfileInputEl=document.querySelector("input[type=file]");constimagePart=awaitfileToGenerativePart(fileInputEl.files[0]);// To generate text output, call `generateContent` with the text and imageconstresult=awaitmodel.generateContent([prompt,imagePart]);constresponse=result.response;consttext=response.text();console.log(text);}run();
Note thatFirebase AI Logicalso supports streaming of text responses usinggenerateContentStream(instead ofgenerateContent).
Enable end-users to try your feature
For end-users to try your feature in your app, you mustenroll in the Chrome Origin Trials.
Note that there's a limited duration and usage for these trials.
Features not yet available for on-device inference
As a preview release, not all the capabilities of the Web SDK are available foron-deviceinference.The following features arenot yet supported for on-device inference(but they are usually available for
cloud-based inference).
Generating text from image file input types other than JPEG and PNG
Can fallback to the cloud-hosted model;
however,ONLY_ON_DEVICEmode will throw an error.
Generating text from audio, video, and documents (like PDFs) inputs
Can fallback to the cloud-hosted model;
however,ONLY_ON_DEVICEmode will throw an error.
Generating images usingGeminiorImagenmodels
Can fallback to the cloud-hosted model;
however,ONLY_ON_DEVICEmode will throw an error.
Providing files using URLs in multimodal requests. You must provide files as
inline data to on-device models.
Multi-turn chat
Can fallback to the cloud-hosted model;
however,ONLY_ON_DEVICEmode will throw an error.
Bi-directional streaming with theGemini Live API
Providing the model withtoolsto help it generate its response
(like function calling, code execution, URL context, and
grounding with Google Search)
Count tokens
Always throws an error. The count will differ between cloud-hosted and
on-device models, so there is no intuitive fallback.
AI monitoring in theFirebaseconsole for on-device inference.
Note that any inference using the cloud-hosted models can be
monitored just like other inference using theFirebase AI Logicclient SDK for Web.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-03-20 UTC."],[],[]]