Gemini 3 Pro Image (Nano Banana Pro 🍌) Generation
Source notebook
Repo path:
gemini/getting-started/intro_gemini_3_image_gen.ipynb· Open on GitHub · intro
Shows how to generate and edit images with Gemini 3 Pro Image using the Google GenAI SDK.
Summary
This notebook teaches how to use Nano Banana Pro, loaded as gemini-3-pro-image, on Agent Platform with the Google Gen AI SDK. It walks through text-to-image generation, model thoughts, Google Search grounding, aspect ratios, image sizes, localization, multi-turn image editing, and multiple reference images. The workflow sets up a project client, calls generate_content with image configs, inspects response parts, and passes image bytes or Cloud Storage URIs for editing.
Key code patterns
Enterprise client setup
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = "global"
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION,
)Creates the Google GenAI SDK client for Agent Platform using a project and global location.
Text-to-image generation
response = client.models.generate_content(
model="gemini-3-pro-image",
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=["IMAGE", "TEXT"],
image_config=types.ImageConfig(aspect_ratio="16:9"),
),
)Uses response_modalities and ImageConfig to request generated image output.
Finish reason guard
if response.candidates[0].finish_reason != types.FinishReason.STOP:
reason = response.candidates[0].finish_reason
raise ValueError(f"Prompt Content Error: {reason}")Checks for prompt or content errors when an image is not generated.
Display image parts
for part in response.candidates[0].content.parts:
if part.thought:
continue
if part.inline_data:
display(Image(data=part.inline_data.data, width=1000))Iterates response parts and skips thought content before displaying image bytes.
Google Search grounding
google_search = types.Tool(google_search=types.GoogleSearch())
response = client.models.generate_content(
model=MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
tools=[google_search],
),
)Adds a Google Search tool so the generated response can be grounded in search text results.
Image editing from bytes
with open(starting_image, "rb") as f:
image = f.read()
response = client.models.generate_content(
model=MODEL_ID,
contents=[
types.Part.from_bytes(data=image, mime_type="image/png"),
"Change the text in this infographic from English to Spanish.",
],
)Passes an input image and text instruction for localization through image editing.
Multi-turn image editing
chat = client.chats.create(
model=MODEL_ID,
config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)
response = chat.send_message(message)
response = chat.send_message([
types.Part.from_bytes(data=data, mime_type="image/png"),
"Make the perfume bottle purple.",
])Keeps a chat session and feeds prior image bytes into the next edit request.
Multiple reference images
response = client.models.generate_content(
model=MODEL_ID,
contents=[
types.Part.from_uri(file_uri="gs://cloud-samples-data/generative-ai/image/woman.jpg", mime_type="image/jpeg"),
types.Part.from_uri(file_uri="gs://cloud-samples-data/generative-ai/image/suitcase.png", mime_type="image/png"),
prompt,
],
)Uses Cloud Storage URIs as multiple reference images for a composed generation.
Models & APIs used
- Models: gemini-3-pro-image
- APIs / services: Agent Platform API, Cloud Storage, Google Search
- SDKs / libraries:
google-genai,IPython,matplotlib,requests,Pillow
When to use this
Use this pattern when building Gemini image generation and editing workflows that need text prompts, search grounding, iterative edits, or reference images.
Gotchas & caveats
- The notebook requires an existing Google Cloud project and the Agent Platform API enabled.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- The client uses LOCATION = “global”.
- To generate images, IMAGE must be included in response_modalities.
- Valid aspect ratios are limited to 1:1, 3:2, 2:3, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, and 21:9.
- Supported image sizes are 1K, 2K, and 4K.
- Google Search grounding is based on text results, not images from Google Search.
- The notebook notes higher latency than Gemini 2.5 Flash Image.
- All generated images include a SynthID watermark.
Best practices
- Install or upgrade google-genai before running the notebook.
- Fallback to the GOOGLE_CLOUD_PROJECT environment variable when PROJECT_ID is not set.
- Use GenerateContentConfig with response_modalities and ImageConfig for image output control.
- Check finish_reason before assuming an image was generated.
- Skip thought parts when displaying final generated images.
- Display grounding citations and sources from grounding_metadata.
- Use Part.from_bytes for local image editing inputs.
- Use Part.from_uri for Cloud Storage reference images.
Related
- Concepts: Getting Started · Image & Video Generation · RAG & Grounding
- Entities: Google GenAI SDK · Cloud Storage · Grounding · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Image & Video Generation - Best Practices · RAG & Grounding - Best Practices