The Firebase AI Logic SDKs give you access to the Imagen models (via the Imagen API ) so that you can generate images from a text prompt. With this capability, you can do things like:
- Generate images from prompts written in natural language
- Generate images in a wide range of formats and styles
- Render text in images
Note that Firebase AI Logic doesn't yet support all the features available for the Imagen models. Learn more in Supported capabilities and features later on this page.
Jump to code for text-only input
Choosing between Gemini and Imagen models
The Firebase AI Logic SDKs support image generation using either a Gemini model or an Imagen model. For most use cases, start with Gemini , and then choose Imagen for specialized tasks where image quality is critical.
Note that the Firebase AI Logic SDKs do not yet support image input (like for editing) with Imagen models. So, if you want to work with input images, you can use a Gemini model instead.
Choose Gemini when you want:
- To use world knowledge and reasoning to generate contextually relevant images.
- To seamlessly blend text and images or to interleave text and image output.
- To embed accurate visuals within long text sequences.
- To edit images conversationally while maintaining context.
Choose Imagen when you want:
- To prioritize image quality, photorealism, artistic detail, or specific styles (for example, impressionism or anime).
- To explicitly specify the aspect ratio or format of generated images.
Before you begin
Click your Gemini API provider to view provider-specific content and code on this page.
If you haven't already, complete the getting started guide
, which describes how to
set up your Firebase project, connect your app to Firebase, add the SDK,
initialize the backend service for your chosen API provider, and
create an ImagenModel
instance.
Models that support this capability
The Gemini Developer API supports image generation by the latest stable Imagen models. This limitation of supported Imagen models is applicable regardless of how you access the Gemini Developer API .
-
imagen-4.0-generate-001
-
imagen-4.0-fast-generate-001
-
imagen-4.0-ultra-generate-001
-
imagen-3.0-generate-002
Generate images from text-only input
You can ask an Imagen model to generate images by prompting with text. You can generate one image or multiple images .
You can also set many different configuration options for image generation , like aspect ratio and image format.
Generate one image from text-only input
In that section, you'll also click a button for your chosen Gemini API provider so that you see provider-specific content on this page.
You can ask an Imagen model to generate a single image by prompting with text.
Make sure to create an ImagenModel
instance and call generateImages
.
Swift
import
FirebaseAI
// Initialize the Gemini Developer API backend service
let
ai
=
FirebaseAI
.
firebaseAI
(
backend
:
.
googleAI
())
// Create an `ImagenModel` instance with a model that supports your use case
let
model
=
ai
.
imagenModel
(
modelName
:
"imagen-4.0-generate-001"
)
// Provide an image generation prompt
let
prompt
=
"An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
let
response
=
try
await
model
.
generateImages
(
prompt
:
prompt
)
// Handle the generated image
guard
let
image
=
response
.
images
.
first
else
{
fatalError
(
"No image in the response."
)
}
let
uiImage
=
UIImage
(
data
:
image
.
data
)
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn
(
PublicPreviewAPI
::
class
)
suspend
fun
generateImage
()
{
// Initialize the Gemini Developer API backend service
val
ai
=
Firebase
.
ai
(
backend
=
GenerativeBackend
.
googleAI
())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val
model
=
ai
.
imagenModel
(
"imagen-4.0-generate-001"
)
// Provide an image generation prompt
val
prompt
=
"An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
val
imageResponse
=
model
.
generateImages
(
prompt
)
// Handle the generated image
val
image
=
imageResponse
.
images
.
first
()
val
bitmapImage
=
image
.
asBitmap
()
}
Java
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel
imagenModel
=
FirebaseAI
.
getInstance
(
GenerativeBackend
.
googleAI
())
.
imagenModel
(
/* modelName */
"imagen-4.0-generate-001"
);
ImagenModelFutures
model
=
ImagenModelFutures
.
from
(
imagenModel
);
// Provide an image generation prompt
String
prompt
=
"An astronaut riding a horse"
;
// To generate an image, call `generateImages` with the text prompt
Futures
.
addCallback
(
model
.
generateImages
(
prompt
),
new
FutureCallback<ImagenGenerationResponse<ImagenInlineImage>
> ()
{
@Override
public
void
onSuccess
(
ImagenGenerationResponse<ImagenInlineImage>
result
)
{
if
(
result
.
getImages
().
isEmpty
())
{
Log
.
d
(
"TAG"
,
"No images generated"
);
}
Bitmap
bitmap
=
result
.
getImages
().
get
(
0
).
asBitmap
();
// Use the bitmap to display the image in your UI
}
@Override
public
void
onFailure
(
Throwable
t
)
{
// ...
}
},
Executors
.
newSingleThreadExecutor
());
Web
import
{
initializeApp
}
from
"firebase/app"
;
import
{
getAI
,
getGenerativeModel
,
GoogleAIBackend
}
from
"firebase/ai"
;
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const
firebaseConfig
=
{
// ...
};
// Initialize FirebaseApp
const
firebaseApp
=
initializeApp
(
firebaseConfig
);
// Initialize the Gemini Developer API backend service
const
ai
=
getAI
(
firebaseApp
,
{
backend
:
new
GoogleAIBackend
()
});
// Create an `ImagenModel` instance with an Imagen model that supports your use case
const
model
=
getImagenModel
(
ai
,
{
model
:
"imagen-4.0-generate-001"
});
// Provide an image generation prompt
const
prompt
=
"An astronaut riding a horse."
;
// To generate an image, call `generateImages` with the text prompt
const
response
=
await
model
.
generateImages
(
prompt
)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
response
.
filteredReason
)
{
console
.
log
(
response
.
filteredReason
);
}
if
(
response
.
images
.
length
==
0
)
{
throw
new
Error
(
"No images in the response."
)
}
const
image
=
response
.
images
[
0
];
Dart
import
'package:firebase_ai/firebase_ai.dart'
;
import
'package:firebase_core/firebase_core.dart'
;
import
'firebase_options.dart'
;
// Initialize FirebaseApp
await
Firebase
.
initializeApp
(
options:
DefaultFirebaseOptions
.
currentPlatform
,
);
// Initialize the Gemini Developer API backend service
final
model
=
FirebaseAI
.
googleAI
();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final
model
=
ai
.
imagenModel
(
model:
'imagen-4.0-generate-001'
);
// Provide an image generation prompt
const
prompt
=
'An astronaut riding a horse.'
;
// To generate an image, call `generateImages` with the text prompt
final
response
=
await
model
.
generateImages
(
prompt
);
if
(
response
.
images
.
isNotEmpty
)
{
final
image
=
response
.
images
[
0
];
// Process the image
}
else
{
// Handle the case where no images were generated
print
(
'Error: No images were generated.'
);
}
Unity
using
Firebase.AI
;
// Initialize the Gemini Developer API backend service
var
ai
=
FirebaseAI
.
GetInstance
(
FirebaseAI
.
Backend
.
GoogleAI
());
// Create an `ImagenModel` instance with a model that supports your use case
var
model
=
ai
.
GetImagenModel
(
modelName
:
"imagen-4.0-generate-001"
);
// Provide an image generation prompt
var
prompt
=
"An astronaut riding a horse"
;
// To generate an image, call `generateImages` with the text prompt
var
response
=
await
model
.
GenerateImagesAsync
(
prompt
:
prompt
);
// Handle the generated image
if
(
response
.
Images
.
Count
==
0
)
{
throw
new
Exception
(
"No image in the response."
);
}
var
image
=
response
.
Images
[
0
].
AsTexture2D
();
Learn how to choose a model appropriate for your use case and app.
Generate multiple images from text-only input
In that section, you'll also click a button for your chosen Gemini API provider so that you see provider-specific content on this page.
By default, Imagen
models generate only one image per request.
However, you can ask an Imagen
model to generate multiple images
per request by providing an ImagenGenerationConfig
when creating the ImagenModel
instance.
Make sure to create an ImagenModel
instance and call generateImages
.
Swift
import
FirebaseAI
// Initialize the Gemini Developer API backend service
let
ai
=
FirebaseAI
.
firebaseAI
(
backend
:
.
googleAI
())
// Create an `ImagenModel` instance with a model that supports your use case
let
model
=
ai
.
imagenModel
(
modelName
:
"imagen-4.0-generate-001"
,
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig
:
ImagenGenerationConfig
(
numberOfImages
:
4
)
)
// Provide an image generation prompt
let
prompt
=
"An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
let
response
=
try
await
model
.
generateImages
(
prompt
:
prompt
)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
let
filteredReason
=
response
.
filteredReason
{
print
(
filteredReason
)
}
// Handle the generated images
let
uiImages
=
response
.
images
.
compactMap
{
UIImage
(
data
:
$0
.
data
)
}
Kotlin
// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn
(
PublicPreviewAPI
::
class
)
suspend
fun
generateImage
()
{
// Initialize the Gemini Developer API backend service
val
ai
=
Firebase
.
ai
(
backend
=
GenerativeBackend
.
googleAI
())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
val
model
=
ai
.
imagenModel
(
modelName
=
"imagen-4.0-generate-001"
,
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig
=
ImagenGenerationConfig
(
numberOfImages
=
4
)
)
// Provide an image generation prompt
val
prompt
=
"An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
val
imageResponse
=
model
.
generateImages
(
prompt
)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
imageResponse
.
filteredReason
!=
null
)
{
Log
.
d
(
TAG
,
"FilteredReason:
${
imageResponse
.
filteredReason
}
"
)
}
for
(
image
in
imageResponse
.
images
)
{
val
bitmap
=
image
.
asBitmap
()
// Use the bitmap to display the image in your UI
}
}
Java
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
ImagenGenerationConfig
imagenGenerationConfig
=
new
ImagenGenerationConfig
.
Builder
()
.
setNumberOfImages
(
4
)
.
build
();
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel
imagenModel
=
FirebaseAI
.
getInstance
(
GenerativeBackend
.
googleAI
())
.
imagenModel
(
/* modelName */
"imagen-4.0-generate-001"
,
/* imageGenerationConfig */
imagenGenerationConfig
);
ImagenModelFutures
model
=
ImagenModelFutures
.
from
(
imagenModel
);
// Provide an image generation prompt
String
prompt
=
"An astronaut riding a horse"
;
// To generate images, call `generateImages` with the text prompt
Futures
.
addCallback
(
model
.
generateImages
(
prompt
),
new
FutureCallback<ImagenGenerationResponse<ImagenInlineImage>
> ()
{
@Override
public
void
onSuccess
(
ImagenGenerationResponse<ImagenInlineImage>
result
)
{
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
result
.
getFilteredReason
()
!=
null
){
Log
.
d
(
"TAG"
,
"FilteredReason: "
+
result
.
getFilteredReason
());
}
// Handle the generated images
List<ImagenInlineImage>
images
=
result
.
getImages
();
for
(
ImagenInlineImage
image
:
images
)
{
Bitmap
bitmap
=
image
.
asBitmap
();
// Use the bitmap to display the image in your UI
}
}
@Override
public
void
onFailure
(
Throwable
t
)
{
// ...
}
},
Executors
.
newSingleThreadExecutor
());
Web
import
{
initializeApp
}
from
"firebase/app"
;
import
{
getAI
,
getGenerativeModel
,
GoogleAIBackend
}
from
"firebase/ai"
;
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const
firebaseConfig
=
{
// ...
};
// Initialize FirebaseApp
const
firebaseApp
=
initializeApp
(
firebaseConfig
);
// Initialize the Gemini Developer API backend service
const
ai
=
getAI
(
firebaseApp
,
{
backend
:
new
GoogleAIBackend
()
});
// Create an `ImagenModel` instance with an Imagen model that supports your use case
const
model
=
getImagenModel
(
ai
,
{
model
:
"imagen-4.0-generate-001"
,
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig
:
{
numberOfImages
:
4
}
}
);
// Provide an image generation prompt
const
prompt
=
"An astronaut riding a horse."
;
// To generate images, call `generateImages` with the text prompt
const
response
=
await
model
.
generateImages
(
prompt
)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
response
.
filteredReason
)
{
console
.
log
(
response
.
filteredReason
);
}
if
(
response
.
images
.
length
==
0
)
{
throw
new
Error
(
"No images in the response."
)
}
const
images
=
response
.
images
[
0
];
Dart
import
'package:firebase_ai/firebase_ai.dart'
;
import
'package:firebase_core/firebase_core.dart'
;
import
'firebase_options.dart'
;
// Initialize FirebaseApp
await
Firebase
.
initializeApp
(
options:
DefaultFirebaseOptions
.
currentPlatform
,
);
// Initialize the Gemini Developer API backend service
final
ai
=
FirebaseAI
.
googleAI
();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final
model
=
ai
.
imagenModel
(
model:
'imagen-4.0-generate-001'
,
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig:
ImagenGenerationConfig
(
numberOfImages:
4
),
);
// Provide an image generation prompt
const
prompt
=
'An astronaut riding a horse.'
;
// To generate images, call `generateImages` with the text prompt
final
response
=
await
model
.
generateImages
(
prompt
);
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
response
.
filteredReason
!=
null
)
{
print
(
response
.
filteredReason
);
}
if
(
response
.
images
.
isNotEmpty
)
{
final
images
=
response
.
images
;
for
(
var
image
in
images
)
{
// Process the image
}
}
else
{
// Handle the case where no images were generated
print
(
'Error: No images were generated.'
);
}
Unity
using
Firebase.AI
;
// Initialize the Gemini Developer API backend service
var
ai
=
FirebaseAI
.
GetInstance
(
FirebaseAI
.
Backend
.
GoogleAI
());
// Create an `ImagenModel` instance with a model that supports your use case
var
model
=
ai
.
GetImagenModel
(
modelName
:
"imagen-4.0-generate-001"
,
// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
generationConfig
:
new
ImagenGenerationConfig
(
numberOfImages
:
4
)
);
// Provide an image generation prompt
var
prompt
=
"An astronaut riding a horse"
;
// To generate an image, call `generateImages` with the text prompt
var
response
=
await
model
.
GenerateImagesAsync
(
prompt
:
prompt
);
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if
(
!
string
.
IsNullOrEmpty
(
response
.
FilteredReason
))
{
UnityEngine
.
Debug
.
Log
(
"Filtered reason: "
+
response
.
FilteredReason
);
}
// Handle the generated images
var
images
=
response
.
Images
.
Select
(
image
=>
image
.
AsTexture2D
());
Learn how to choose a model appropriate for your use case and app.
Supported features and requirements
The Imagen models offer many features related to image generation. This section describes what's supported when using the models with Firebase AI Logic .
Supported capabilities and features
Firebase AI Logic supports these features of Imagen models.
-
Generating people and faces (given that your Firebase project has approval from Google Cloud )
-
Generating text within generated images
-
Adding a watermark to generated images
-
Verifying digital watermarks when using the Vertex AI Gemini API
If you want to verify that an image has a watermark, you can upload the image into Vertex AI Studio using its Mediatab. -
Configuring image generation parameters , like number of generated images, aspect ratio, and watermarking
-
Configuring safety settings
Firebase AI Logic does not support these advanced features of Imagen models.
Note that most of these features require being on an approved list of users even when using Imagen models server-side.
-
Image editing or manipulation features, which includes upscaling images
-
Including images in the request to the model (like for few-shot learning)
-
Enabling
includeSafetyAttributes
, which means thatsafetyAttributes.categories
andsafetyAttributes.scores
cannot be returned -
Disabling prompt rewriter (the
enhancePrompt
parameter). This means that an LLM-based prompt rewriting tool will always automatically add more detail to the provided prompt to deliver higher quality images that better reflect the prompt provided. -
Writing a generated image directly into Google Cloud Storage as part of the response from the model (the
storageUri
parameter). Instead, images are always returned as base64-encoded image bytes in the response.
If you want to upload a generated image to Cloud Storage , you can use Cloud Storage for Firebase .
Specifications and limitations
- 1024x1024 pixels (1:1 aspect ratio)
- 896x1280 (3:4 aspect ratio)
- 1280x896 (4:3 aspect ratio)
- 768x1408 (9:16 aspect ratio)
- 1408x768 (16:9 aspect ratio)
What else can you do?
- Start thinking about preparing for production (see the production checklist
),
including:
- Setting up Firebase App Check to protect the Gemini API from abuse by unauthorized clients.
- Integrating Firebase Remote Config to update values in your app (like model name) without releasing a new app version.
Learn how to control content generation
- Understand prompt design , including best practices, strategies, and example prompts.
- Configure Imagen model parameters like aspect ratio, person generation, and watermarking.
- Use safety settings to adjust the likelihood of getting responses that may be considered harmful.
Learn more about the supported models
Learn about the models available for various use cases and their quotas and pricing .Give feedback about your experience with Firebase AI Logic