Gemini 2.5 Flash Image (Nano Banana 🍌) Generation
Source notebook
Repo path:
gemini/getting-started/intro_gemini_2_5_image_gen.ipynb· Open on GitHub · intermediate
Generates and edits images with Gemini 2.5 Flash Image using the Google Gen AI SDK on Agent Platform.
Summary
This notebook teaches how to use Gemini 2.5 Flash Image for text-to-image generation, interleaved text-and-image responses, and image editing. It demonstrates local byte uploads, Cloud Storage URI inputs, multi-turn chat editing, multiple reference images, aspect ratio configuration, and response part rendering.
Key code patterns
Create Gen AI client
from google import genai
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location="global",
)Initializes the Google Gen AI SDK client for Agent Platform with a project and global location.
Text-to-image generation
response = client.models.generate_content(
model=MODEL_ID,
contents="a cartoon infographic on flying sneakers",
config=GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=ImageConfig(aspect_ratio="9:16"),
candidate_count=1,
),
)Shows the minimal pattern for requesting generated images and controlling aspect ratio.
Check finish reason
if response.candidates[0].finish_reason != FinishReason.STOP:
reason = response.candidates[0].finish_reason
raise ValueError(f"Prompt Content Error: {reason}")Validates that image generation completed successfully before reading response parts.
Interleaved text and image
response = client.models.generate_content(
model=MODEL_ID,
contents="Create a tutorial...",
config=GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
image_config=ImageConfig(aspect_ratio="4:3"),
),
)Requests both text and image modalities so the model can return mixed content parts.
Local image editing
with open(subject_image, "rb") as f:
image = f.read()
contents = [
Part.from_bytes(data=image, mime_type="image/jpeg"),
"Create a pencil sketch image of this dog...",
]Uses Part.from_bytes to send a local image as an editing reference.
Cloud Storage image input
message = [
Part.from_uri(
file_uri=perfume_uri,
mime_type="image/jpeg",
),
"change the perfume color to a light purple",
]Uses Part.from_uri to reference images stored in Cloud Storage without local upload.
Multi-turn editing chat
chat = client.chats.create(model=MODEL_ID)
response = chat.send_message(
message=[Part.from_uri(file_uri=perfume_uri, mime_type="image/jpeg"), prompt],
config=GenerateContentConfig(response_modalities=["IMAGE"]),
)Maintains conversational context for iterative image edits.
Multiple reference images
contents = [
Part.from_uri(file_uri="gs://.../suitcase.png", mime_type="image/png"),
Part.from_uri(file_uri="gs://.../woman.jpg", mime_type="image/jpeg"),
"Generate an image...",
]Combines multiple input images in one generation request.
Models & APIs used
- Models: gemini-2.5-flash-image
- APIs / services: Vertex AI, Agent Platform, Cloud Storage
- SDKs / libraries:
google-genai,matplotlib,requests,IPython,Pillow
When to use this
Use this pattern when building image generation or conversational image editing workflows with Gemini on Agent Platform.
Gotchas & caveats
- Agent Platform API must be enabled for the Google Cloud project.
- Colab requires notebook authentication with google.colab.auth.authenticate_user().
- PROJECT_ID must be set directly or via GOOGLE_CLOUD_PROJECT.
- The notebook uses LOCATION = “global”.
- Image generation requires IMAGE in response_modalities.
- For text plus image output, response_modalities must include both TEXT and IMAGE.
- Valid aspect ratios are listed explicitly in the notebook.
- Gemini 2.5 Flash Image retirement on Gemini Enterprise Agent Platform is stated as October 2, 2026.
- Generated images include a SynthID watermark.
Best practices
- Set response_modalities to match the desired output modalities.
- Use ImageConfig to specify aspect_ratio for generated images.
- Check finish_reason before assuming an image was generated.
- Iterate over response content parts and handle both text and inline_data.
- Use Part.from_bytes for local image inputs.
- Use Part.from_uri for Cloud Storage image inputs.
- Use a chat session for multi-turn image editing.
- Supply multiple Part.from_uri instances when editing with multiple reference images.
Related
- Concepts: Getting Started · Gemini Capabilities · Image & Video Generation
- Entities: Vertex AI · Google GenAI SDK · Cloud Storage · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Gemini Capabilities - Best Practices · Image & Video Generation - Best Practices