Nano Banana

NanoBananaTools enables an Agent to generate images using Google's Gemini 2.5 Flash Image model (Nano Banana).

Prerequisites

You need to install the google-genai and Pillow libraries.

1uv pip install google-genai Pillow

Set the GOOGLE_API_KEY environment variable. You can obtain an API key from Google AI Studio.

1export GOOGLE_API_KEY=****

Example

The following agent will use Nano Banana to generate an image based on a text prompt.

1from pathlib import Path
2
3from kern.agent import Agent
4from kern.tools.nano_banana import NanoBananaTools
5
6# Example 1: Basic NanoBanana agent with default settings
7agent = Agent(tools=[NanoBananaTools()], name="NanoBanana Image Generator")
8
9# Example 2: Custom aspect ratio generator
10portrait_agent = Agent(
11 tools=[
12 NanoBananaTools(
13 aspect_ratio="2:3" # Portrait orientation
14 )
15 ],
16 name="Portrait NanoBanana Generator",
17)
18
19# Example 3: Widescreen generator for panoramic images
20widescreen_agent = Agent(
21 tools=[
22 NanoBananaTools(
23 aspect_ratio="16:9" # Widescreen format
24 )
25 ],
26 name="Widescreen NanoBanana Generator",
27)
28
29# Test basic generation
30agent.print_response(
31 "Generate an image of a futuristic city with flying cars",
32 markdown=True,
33)
34
35# Generate and save an image
36response = widescreen_agent.run(
37 "Create a panoramic nature scene with mountains and a lake at sunset",
38 markdown=True,
39)
40
41# Save the generated image if available
42if response.images and response.images[0].content:
43 output_path = Path("generated_image.png")
44 with open(output_path, "wb") as f:
45 f.write(response.images[0].content)
46
47 print(f"Image was successfully generated and saved to: {output_path}")

Toolkit Params

ParameterTypeDefaultDescription
modelstr"gemini-2.5-flash-image"The Nano Banana model to use
aspect_ratiostr"1:1"Image aspect ratio. Supported: 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
api_keystrNoneThe Google API key for authentication
enable_create_imageboolTrueEnable the create image functionality

Toolkit Functions

FunctionDescription
create_imageGenerates an image based on a text prompt

Developer Resources