Skip to main content

Overview

Nano Banana (codename) is Google’s image generation model codename, currently with two available versions:
  • Official Release: gemini-2.5-flash-image (recommended, supports 10 aspect ratios and custom resolution)
  • Preview Version: gemini-2.5-flash-image-preview (still available)
Both models have powerful image generation and editing capabilities, with identical pricing and calling methods.
October 3, 2025 Update: Google released the official version gemini-2.5-flash-image, adding 10 aspect ratio support and custom resolution features. Recommend using the official version for best experience, while the old version remains usable.

📚 Latest Documentation

Feishu Complete Usage Guide - Fastest updates, supports comment interactionVisit Feishu documentation for latest tutorials, tips sharing, and Q&A. Documentation continuously updated, questions can be discussed directly in Feishu comments.

Core Advantages
  • 🚀 Fast Generation: Average only 10 seconds, faster than OpenAI series
  • 💰 Pricing Advantage: $0.025/image (50% off official), about ¥0.14/image with top-up bonuses
  • 🔄 Full Compatibility: Uses chat completion endpoint, fully compatible with gpt-4o-image and sora_image calling methods
  • 🎨 Google Technology: Based on Google’s latest and strongest image generation/editing technology

Calling Methods

Correct Endpoint ✅

POST /v1/chat/completions

Incorrect Endpoint ❌

POST /v1/images/generations  # This endpoint not supported
Important Note: This model uses the chat completion endpoint, not the traditional image generation endpoint. This design maintains compatibility with models like gpt-4o-image and sora_image.

Compatibility Notes

If you’ve previously used the following models, simply replace the model name:
  • gpt-4o-imagegemini-2.5-flash-image (recommended) or gemini-2.5-flash-image-preview
  • sora_imagegemini-2.5-flash-image (recommended) or gemini-2.5-flash-image-preview
Keep other parameters unchanged for seamless switching.
Recommend Official Version: gemini-2.5-flash-image supports more features (10 aspect ratios, custom resolution), but preview version gemini-2.5-flash-image-preview still works normally.

Python Example Code

Complete Python example code with automatic local saving of base64 images:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Gemini Image Generation - Python Version
Supports non-streaming output and automatic saving of base64 images locally
"""

import requests
import json
import base64
import re
import os
import datetime
from typing import Optional, Tuple

class GeminiImageGenerator:
    def __init__(self, api_key: str, api_url: str = "https://api.apiyi.com/v1/chat/completions"):
        """
        Initialize Gemini Image Generator

        Args:
            api_key: API key
            api_url: API address
        """
        self.api_key = api_key
        self.api_url = api_url
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }

    def generate_image(self, prompt: str, model: str = "gemini-2.5-flash-image",
                      output_dir: str = ".") -> Tuple[bool, str]:
        """
        Generate image and save locally

        Args:
            prompt: Image description prompt
            model: Model to use
            output_dir: Output directory

        Returns:
            Tuple[success status, result message]
        """
        print("🚀 Starting image generation...")
        print(f"Prompt: {prompt}")
        print(f"Model: {model}")

        # Generate filename
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        output_file = os.path.join(output_dir, f"gemini_generated_{timestamp}.png")

        try:
            # Prepare request data
            payload = {
                "model": model,
                "stream": False,
                "messages": [
                    {
                        "role": "user",
                        "content": prompt
                    }
                ]
            }

            print("📡 Sending API request...")

            # Send non-streaming request
            response = requests.post(
                self.api_url,
                headers=self.headers,
                json=payload,
                timeout=300
            )

            if response.status_code != 200:
                error_msg = f"API request failed, status code: {response.status_code}"
                try:
                    error_detail = response.json()
                    error_msg += f", error details: {error_detail}"
                except:
                    error_msg += f", response content: {response.text[:500]}"
                return False, error_msg

            print("✅ API request successful, parsing response...")

            # Parse non-streaming JSON response
            try:
                result = response.json()
                print("✅ Successfully parsed JSON response")
            except json.JSONDecodeError as e:
                return False, f"JSON parsing failed: {str(e)}"

            # Extract message content
            full_content = ""
            if "choices" in result and len(result["choices"]) > 0:
                choice = result["choices"][0]
                if "message" in choice and "content" in choice["message"]:
                    full_content = choice["message"]["content"]

            if not full_content:
                return False, "Message content not found"

            print(f"📝 Got message content, length: {len(full_content)} characters")
            print("🔍 Parsing image data...")

            # Extract and save image (supports both base64 and URL)
            success, message = self._extract_and_save_images(full_content, output_file)

            if success:
                return True, message
            else:
                return False, f"Image save failed: {message}"

        except requests.exceptions.Timeout:
            return False, "Request timeout (300 seconds)"
        except requests.exceptions.ConnectionError as e:
            return False, f"Connection error: {str(e)}"
        except Exception as e:
            return False, f"Unknown error: {str(e)}"

    def _extract_and_save_images(self, content: str, base_output_file: str) -> Tuple[bool, str]:
        """
        Efficiently extract and save base64 image data

        Args:
            content: Content containing image data
            base_output_file: Base output file path

        Returns:
            Tuple[success status, result message]
        """
        try:
            print(f"📄 Content preview (first 200 chars): {content[:200]}")

            # Use precise regex to extract base64 image data in one go
            base64_pattern = r'data:image/([^;]+);base64,([A-Za-z0-9+/=]+)'
            match = re.search(base64_pattern, content)

            if not match:
                print('⚠️  Base64 image data not found')
                return False, "Response does not contain base64 image data"

            image_format = match.group(1)  # png, jpg, etc.
            b64_data = match.group(2)

            print(f'🎨 Image format: {image_format}')
            print(f'📏 Base64 data length: {len(b64_data)} characters')

            # Decode and save image
            image_data = base64.b64decode(b64_data)

            if len(image_data) < 100:
                return False, "Decoded image data too small, may be invalid"

            # Set file extension based on detected format
            output_file = base_output_file.replace('.png', f'.{image_format}')
            os.makedirs(os.path.dirname(output_file) if os.path.dirname(output_file) else ".", exist_ok=True)

            with open(output_file, 'wb') as f:
                f.write(image_data)

            print(f'🖼️  Image saved successfully: {output_file}')
            print(f'📊 File size: {len(image_data)} bytes')

            return True, f"Image saved successfully: {output_file}"

        except Exception as e:
            return False, f"Error processing image: {str(e)}"

def main():
    """
    Main function
    """
    # Configuration parameters
    API_KEY = "sk-"  # Replace with your actual API key
    PROMPT = "a beautiful cat under the tree"

    print("="*60)
    print("Gemini Image Generator - Python Version")
    print("="*60)
    print(f"Start time: {datetime.datetime.now()}")

    # Create generator instance
    generator = GeminiImageGenerator(API_KEY)

    # Generate image
    success, message = generator.generate_image(PROMPT)

    print("\n" + "="*60)
    if success:
        print("🎉 Execution successful!")
        print(f"✅ {message}")
    else:
        print("❌ Execution failed!")
        print(f"💥 {message}")

    print(f"End time: {datetime.datetime.now()}")
    print("="*60)

if __name__ == "__main__":
    main()

Usage Steps

  1. Replace API Key: Replace API_KEY in code with your actual API key
  2. Modify Prompt: Modify PROMPT variable as needed
  3. Run Script: Execute Python script, image will be automatically saved locally

Price Comparison

ModelPricingAdvantage
Nano Banana$0.025/image (~¥0.14 with bonuses)⭐ Google’s strongest tech, fast generation
gpt-image-1Higher-
flux-kontext-pro$0.035/imageOn par
sora_imageLowerReverse-engineered model, moderate stability
Cost-effectiveness Recommendation: Nano Banana achieves good balance between price, speed, and quality, especially suitable for applications requiring fast generation of high-quality images.

Feature Comparison

Speed Comparison

  • Nano Banana: Average 10 seconds
  • OpenAI Series: 15-30 seconds
  • Other Models: Varies by model

Compatibility

  • ✅ Fully compatible with gpt-4o-image calling method
  • ✅ Fully compatible with sora_image calling method
  • ✅ Supports chat completion endpoint
  • ❌ Does not support traditional image generation endpoint

FAQ

To maintain compatibility with existing image generation models (like gpt-4o-image, sora_image), making it convenient for users to seamlessly switch between different image generation models.
Simply change the model name from gpt-4o-image or sora_image to gemini-2.5-flash-image (recommended) or gemini-2.5-flash-image-preview, keeping other parameters unchanged.
Official version gemini-2.5-flash-image supports 10 aspect ratios and custom resolution features, while preview version gemini-2.5-flash-image-preview has fewer features but works normally. Both have identical pricing and calling methods, official version recommended.
The model returns base64-encoded image data, typically in PNG or JPEG format. Code automatically detects format and saves to corresponding file type.
Yes, Nano Banana supports not only image generation but also image editing features. Please refer to detailed documentation for specific usage methods.
For more technical details and use cases, check the detailed usage guide link above.