Gemini 3.1 Flash Image (Nano Banana 2 🍌) Generation

Source notebook

Repo path: gemini/getting-started/intro_gemini_3_1_flash_image_gen.ipynb · Open on GitHub · intermediate

Shows Gemini 3.1 Flash Image generation, grounding, video input, and image editing with Google Gen AI SDK.

Summary

This notebook teaches how to use gemini-3.1-flash-image on Agent Platform through the Google Gen AI SDK. It demonstrates text-to-image generation with image config and thinking config, prominent people blocking, Google Search grounding, image sizing, video-to-image prompts, localization, chat-based multi-turn edits, and reference-image editing.

Key code patterns

Create enterprise GenAI client

PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = "global"
client = genai.Client(
    enterprise=True,
    project=PROJECT_ID,
    location=LOCATION,
)

Configures the Google Gen AI SDK client for Agent Platform with a project and global location.

Generate image with config

response = client.models.generate_content(
    model=MODEL_ID,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE", "TEXT"],
        image_config=types.ImageConfig(aspect_ratio="3:2"),
    ),
)

Shows the core request pattern for image generation and mixed text-image responses.

Enable model thoughts

thinking_config=types.ThinkingConfig(
    include_thoughts=True,
    thinking_level=types.ThinkingLevel.HIGH,
)

Demonstrates requesting and later inspecting thinking parts from the response.

google_search = types.Tool(
    google_search=types.GoogleSearch(
        search_types=types.SearchTypes(web_search=types.WebSearch())
    )
)
response = client.models.generate_content(
    model=MODEL_ID,
    contents=prompt,
    config=config,
    tools=[google_search],
)

Adds web search grounding to image generation prompts and exposes grounding metadata.

Use video input

response = client.models.generate_content(
    model=MODEL_ID,
    contents=[
        types.Part.from_bytes(data=video, mime_type="video/mp4"),
        "Generate an illustration of every animal featured in this video",
    ],
    config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)

Shows multimodal prompting with video bytes to generate a new image.

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(edit_message)

Uses chat state to iteratively edit a previously generated image.

Models & APIs used

  • Models: gemini-3.1-flash-image
  • APIs / services: Agent Platform, Vertex AI, Google Search, Cloud Storage
  • SDKs / libraries: google-genai, IPython, matplotlib, requests, Pillow

When to use this

Use this pattern when building image generation or image editing workflows with Gemini that need multimodal inputs, grounding, sizing, or iterative edits.

Gotchas & caveats

  • A Google Cloud project is required and Agent Platform API must be enabled with aiplatform.googleapis.com.
  • Colab requires explicit auth.authenticate_user().
  • LOCATION is set to global in the notebook.
  • IMAGE must be included in response_modalities to generate an image.
  • Valid aspect_ratio values are explicitly constrained in the notebook.
  • thinking_level is limited in the notebook to HIGH or MINIMAL.
  • Generated images include a SynthID watermark.
  • The prominent people example may return a non-STOP finish reason and finish message instead of an image.
  • Multiple reference image editing supports up to 14 reference images.

Best practices

  • Use types.GenerateContentConfig to set response_modalities, image_config, thinking_config, and tools.
  • Check response.candidates[0].finish_reason before assuming an image was generated.
  • Skip parts marked thought when displaying generated images unless inspecting thoughts intentionally.
  • Use Part.from_bytes for local image or video bytes and Part.from_uri for YouTube or Cloud Storage inputs.
  • Use Google Search tools when prompts need current web or image grounding.
  • Display grounding metadata with citations and grounding chunks when search grounding is used.