Back to Hub

Gemini Image Generator

Sandbox Official API

Generate images from text prompts using Google's Imagen model via the Gemini API. Uploads to artifact storage and returns a URL.

Tony CostanzoTony Costanzo
0 installs
v2.0.0
Mar 27, 2026

Gemini Image Generator

Official Generate images from text prompts using Google's Imagen model via the Gemini API.

What this tool does: Creates images from text descriptions using Google's Imagen 4 model. Supports multiple aspect ratios. Images are uploaded to artifact storage and displayed inline in chat. Runs in a secure sandbox.

Setup

Get a Gemini API key

  1. Go to https://aistudio.google.com/apikey
  2. Create a Gemini API key
  3. Imagen is included with the Gemini API

Add workspace secret

Go to Settings > Integrations and create a new secret:

Secret NameDescription
GEMINI_API_KEYYour Google Gemini API key
This is a sandbox tool and Google's image generation has its own costs. Each execution uses workspace credits plus Google's API charges.

Parameters

ParameterRequiredDescription
promptYesText description of the image to generate
aspectRatioNo1:1 (default), 16:9 (landscape), 9:16 (portrait), 3:4, 4:3

Example Prompts

  • "Generate an image of a sunset over mountains in watercolor style"
  • "Create a logo concept for a coffee shop called 'Bean There'"
  • "Make a landscape image of a futuristic city at night"
  • "Generate a portrait-style illustration of a friendly robot"

Tool Definition

Input Parameters

promptstringText description of the image to generate
aspectRatiostringAspect ratio: '1:1' (default), '16:9' (landscape), '9:16' (portrait), '3:4', '4:3'

Sandbox Configuration

Language: python
Timeout: 60s

Code

import json
import urllib.request
import urllib.error
import os
import base64

data = TOOL_INPUT
prompt = data.get('prompt', '')
aspect_ratio = data.get('aspectRatio', '1:1')

if not prompt:
    print('Error: prompt is required')
else:
    api_key = os.environ.get('GEMINI_API_KEY', '')
    if not api_key:
        print('Error: GEMINI_API_KEY secret is required. Add it in Settings > Secrets.')
    else:
        try:
            url = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-4.0-generate-001:predict'
            payload = json.dumps({
                'instances': [{'prompt': prompt}],
                'parameters': {
                    'sampleCount': 1,
                    'aspectRatio': aspect_ratio
                }
            }).encode('utf-8')

            req = urllib.request.Request(url, data=payload, headers={
                'Content-Type': 'application/json',
                'x-goog-api-key': api_key
            }, method='POST')
            with urllib.request.urlopen(req, timeout=50) as resp:
                result = json.loads(resp.read().decode('utf-8'))

            image = result['predictions'][0]
            image_b64 = image.get('bytesBase64Encoded', '')
            mime = image.get('mimeType', 'image/png')
            image_bytes = base64.b64decode(image_b64)

            artifact_key = upload_artifact(image_bytes, mime)
            if artifact_key:
                print(json.dumps({
                    'artifact_key': artifact_key,
                    'contentType': mime,
                    'model': 'imagen-4.0-generate-001',
                    'aspectRatio': aspect_ratio
                }))
            else:
                print(json.dumps({
                    'message': 'Image generated but artifact storage unavailable.',
                    'model': 'imagen-4.0-generate-001',
                    'aspectRatio': aspect_ratio
                }))
        except urllib.error.HTTPError as e:
            err_body = e.read().decode('utf-8') if e.fp else ''
            print(f'Error from Gemini: {e.code} {err_body}')
        except Exception as e:
            print(f'Error: {e}')