Generate & edit images using Gemini (aka "Nano Banana") | Firebase AI Logic
Generate & edit images using Gemini (aka "Nano Banana")
You can ask a Gemini Image model to generate and edit images using both text-only and text-and-file prompts. When you use Firebase AI Logic, you can make this request directly from your app.
With this capability, you can do things like:
- Iteratively generate images through conversation with natural language, adjusting images while maintaining consistency and context.
- Generate images with high-quality text rendering, including long strings of text.
- Generate interleaved text-image output. For example, a blog post with text and images in a single turn. Previously, this required stringing together multiple models.
- Generate images using Gemini's world knowledge and reasoning capabilities.
You can configure the response modality for your request so that the model generates only images or both images and text. Find a complete list of supported features (along with example prompts) later on this page.
Before you begin
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 Gemini API provider, and create a GenerativeModel instance.
Models that support this capability
gemini-3-pro-image(aka "Nano Banana Pro")gemini-3.1-flash-image(aka "Nano Banana 2")gemini-3.1-flash-lite-image(aka "Nano Banana 2 Lite")
Image-generating Gemini 2.5 models support this capability, but they're all deprecated.
Generate and edit images
You can generate and edit images using a Gemini model.
Generate images (text-only input)
You can ask a Gemini Image model to generate images by prompting with text.
Create a GenerativeModel instance, include the response modality of IMAGE in your model configuration, and call generateContent.
import FirebaseAILogic
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a Gemini model that supports image output.
let generativeModel = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "gemini-3.1-flash-image",
// Configure the model to respond with images only.
generationConfig: GenerationConfig(responseModalities: [.image])
)
// Provide a text prompt instructing the model to generate an image
let prompt = "Generate an image of the Eiffel tower with fireworks in the background."
// To generate an image, call `generateContent` with the text input
let response = try await model.generateContent(prompt)
// Handle the generated image
guard let inlineDataPart = response.inlineDataParts.first else {
fatalError("No image data in response.")
}
guard let uiImage = UIImage(data: inlineDataPart.data) else {
fatalError("Failed to convert data to UIImage.")
}
Generate interleaved images and text
You can ask a Gemini Image model to generate interleaved images with its text responses. For example, you can generate images of what each step of a generated recipe might look like along with the step's instructions, and you don't have to make separate requests to the model or different models.
Create a GenerativeModel instance, include response modalities of TEXT and IMAGE in your model configuration, and call generateContent.
let prompt = "Generate an illustrated recipe for a paella. Create images to go alongside the text as you generate the recipe"
// To generate interleaved text and images, call `generateContent` with the text input
let response = try await model.generateContent(prompt)
// Handle the generated text and image
guard let candidate = response.candidates.first else {
fatalError("No candidates in response.")
}
for part in candidate.content.parts {
switch part {
case let textPart as TextPart:
// Do something with the generated text
let text = textPart.text
case let inlineDataPart as InlineDataPart:
// Do something with the generated image
guard let uiImage = UIImage(data: inlineDataPart.data) else {
fatalError("Failed to convert data to UIImage.")
}
default:
fatalError("Unsupported part type: \(part)")
}
}
Edit images (text-and-image input)
You can ask a Gemini Image model to edit images by prompting with text and one or more images.
Create a GenerativeModel instance, include the response modality of IMAGE in your model configuration, and call generateContent.
// Provide an image for the model to edit
let image = UIImage(named: "scones") else { fatalError("Image file not found.") }
// Provide a text prompt instructing the model to edit the image
let prompt = "Edit this image to make it look like a cartoon"
// To edit the image, call `generateContent` with the image and text input
let response = try await model.generateContent(image, prompt)
### Iterate and edit images using multi-turn chat
Using multi-turn chat, you can iterate with a Gemini Image model on the images that it generates or that you supply.
Create a `GenerativeModel` instance, include the response modality of `IMAGE` in your model configuration, and call `startChat()` and `sendMessage()` to send new user messages.
```swift
// Initialize the chat
let chat = model.startChat()
// Provide an initial text prompt instructing the model to edit the image
let prompt = "Edit this image to make it look like a cartoon"
// To generate an initial response, send a user message with the image and text prompt
let response = try await chat.sendMessage(image, prompt)
// Inspect the generated image
guard let inlineDataPart = response.inlineDataParts.first else {
fatalError("No image data in response.")
}
guard let uiImage = UIImage(data: inlineDataPart.data) else {
fatalError("Failed to convert data to UIImage.")
}
// Follow up requests do not need to specify the image again
let followUpResponse = try await chat.sendMessage("But make it old-school line drawing style")
// Inspect the edited image after the follow up request
guard let followUpInlineDataPart = followUpResponse.inlineDataParts.first else {
fatalError("No image data in response.")
}
guard let followUpUIImage = UIImage(data: followUpInlineDataPart.data) else {
fatalError("Failed to convert data to UIImage.")
}
Provide reference images
Gemini Image models let you provide reference images in your prompt. These images can include the following:
- Gemini 3.x Pro Image (
gemini-3-pro-image, aka "Nano Banana Pro")- Up to 6 images of objects with high-fidelity to include in the final image
- Up to 5 images of characters to maintain character consistency
- Up to 3 images to be used as style references
- Gemini 3.x Flash Image (
gemini-3.1-flash-image, aka "Nano Banana 2"):- Up to 10 images of objects with high-fidelity to include in the final image
- Up to 4 images of characters to maintain character consistency
- Gemini 3.x Flash‑Lite Image (
gemini-3.1-flash-lite-image, aka "Nano Banana 2 Lite"):- Up to 10 images of objects with high-fidelity to include in the final image
- Up to 4 images of characters to maintain character consistency
- Gemini 2.5 Flash Image (
gemini-2.5-flash-image, aka "Nano Banana"):- Up to 3 images
Configure image generation
By default, Gemini Image models generate square images (1:1 aspect ratio) at 1024x1024 resolution. You can customize the output of your generated images using the imageConfig property within generationConfig.
For example, you can configure the output image using aspect ratio and image size enums (such as a 16:9 aspect ratio and 2K resolution, resulting in a 2752x1536 image).
Supported aspect ratios
All Gemini Image models support the following aspect ratios:
- Default:
1:1(square)1:1,1:4,1:8,2:3,3:2,3:4,4:1,4:3,4:5,5:4,8:1,9:16,16:9,21:9
Supported image sizes (resolutions)
| Gemini Image model | Supported sizes (resolutions) |
|---|---|
Gemini 3.x Pro Imagegemini-3-pro-image("Nano Banana Pro") |
Default: 1K (1024)1K (1024), 2K (2048), 4K (4096) |
Gemini 3.x Flash Imagegemini-3.1-flash-image("Nano Banana 2") |
Default: 1K (1024)512, 1K (1024), 2K (2048), 4K (4096) |
Gemini 3.x Flash‑Lite Imagegemini-3.1-flash-lite-image("Nano Banana 2 Lite") |
Default: 1K (1024)512 (Gemini Developer API only), 1K (1024) |
Gemini 2.5 Flash Imagegemini-2.5-flash-image("Nano Banana") |
Fixed at 1K (1024) |
Supported features
The following are supported features, like modalities, tools, input, and languages.
Supported modalities
Text arrow_forward Image(s) (text-only to image)
Generate an image of the Eiffel tower with fireworks in the background.
Text arrow_forward Image(s) (text rendering within image)
Generate a cinematic photo of a large building with this giant text projection mapped on the front of the building.
Best practices
- When generating an image containing text, first generate the text and then generate an image with that text.
- Image generation may not always trigger. Also, image or text generation might not work as expected in these situations:
- The model might only generate text and no image (especially if the prompt is ambiguous). If this happens, the
FinishReasonisNO_IMAGE.
- The model might only generate text and no image (especially if the prompt is ambiguous). If this happens, the