All insights

Image generation

Gemini 2.5 Flash Image API: REST, Python, and Production Checks

See how to call Gemini 2.5 Flash Image with the Gemini API, send an image-editing request, handle image output, and check access, input size, quota, and regional availability.

Illustration for Gemini 2.5 flash image api

Direct answer

The Gemini API model ID is gemini-2.5-flash-image. Google lists it as a stable model that supports image generation and text and image input and output. Use the native Gemini API generateContent method for the REST examples below; the code explicitly requests text and image output. (Model documentation · Image-generation guide)

There is an important access caveat: Google says access to Gemini 2.5 models is limited to people who actively used them previously. Google says they are not deprecated and will continue to be served through the API until further notice. So the model ID and stable label do not guarantee that a new project or API key can use it. (Gemini API deprecations)

Generate an image with REST

Create or select a Gemini API key, then keep it in a server-side environment variable. This request uses the v1beta generateContent operation and asks for both text and image parts in the response.

export GEMINI_API_KEY="YOUR_API_KEY"

curl --fail-with-body -sS \
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent" \
  -H "x-goog-api-key: ${GEMINI_API_KEY}" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{
        "text": "Create a photorealistic orange cat sitting on a green velvet chair in a sunlit room."
      }]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"]
    }
  }' > response.json

A successful HTTP response may contain both text and image parts. In the JSON response, look under candidates[].content.parts[]; generated image data is returned in an image part's inlineData.data field, with its MIME type in inlineData.mimeType. Decode that base64 data to save the image. (GenerateContent API reference · Image-generation guide)

import base64
import json

with open("response.json", encoding="utf-8") as f:
    result = json.load(f)

saved = 0
for candidate in result.get("candidates", []):
    for part in candidate.get("content", {}).get("parts", []):
        if "text" in part:
            print(part["text"])
        image = part.get("inlineData")
        if image and image.get("data"):
            mime = image.get("mimeType", "image/png")
            extension = mime.split("/")[-1]
            filename = f"generated-{saved + 1}.{extension}"
            with open(filename, "wb") as out:
                out.write(base64.b64decode(image["data"]))
            print(f"Saved {filename}")
            saved += 1

if saved == 0:
    print("No image part found; inspect the response and any API error details.")

Check for an image part rather than treating any successful HTTP response as proof that an image was generated. If a request fails, inspect the returned status and message; do not blindly retry permission, invalid-key, or unsupported-operation errors. For transient errors such as rate limits, Google recommends exponential backoff. (Troubleshooting guide)

Edit an image with Python

For an edit, send the instruction and image together as content parts. The following uses the Google Gen AI Python SDK's byte-part input pattern and checks each returned part for image data. The SDK pattern is based on Google's documented GenerateContent and image-generation examples; it has not been executed as part of this article. (Gemini 2.5 Flash Image model page · Google's Gemini 2.5 image-generation notebook)

from pathlib import Path
from io import BytesIO
from google import genai
from google.genai import types
from PIL import Image

client = genai.Client()  # Reads GEMINI_API_KEY from the environment
image_bytes = Path("source.jpg").read_bytes()

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=[
        types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"),
        "Remove the background and keep the product unchanged.",
    ],
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
    ),
)

saved = 0
for candidate in response.candidates or []:
    for part in candidate.content.parts:
        if part.text:
            print(part.text)
        elif part.inline_data:
            image = Image.open(BytesIO(part.inline_data.data))
            image.save(f"edited-{saved + 1}.png")
            saved += 1

if saved == 0:
    print("No image part found; inspect the response and any API error details.")

Install the dependencies with pip install -U google-genai pillow. The example shows a single inline JPEG input. For larger inline requests, use the File API instead, and confirm the model remains available to the project making the request.

OpenAI-compatible endpoint: verify before relying on it

A model appearing in a model list does not by itself establish that every endpoint or operation supports it. One developer reported seeing gemini-2.5-flash-image in a model list but receiving a 404 from an OpenAI-compatible /images/generations request. The forum thread did not establish the cause or offer a fix, so it is not proof that the route is categorically unsupported. Use the native Gemini API request above unless current model-specific documentation confirms the OpenAI-compatible operation you need. (Google AI Developers Forum report · OpenAI compatibility documentation)

Availability, input limits, quota, and cost

  • Access: Google’s Gemini API notice restricts access to Gemini 2.5 models to users who actively used them previously. Check that the intended project and key can call the model before building around it. The notice says the models will continue through the API until further notice; it does not promise access for every project. (Deprecations)
  • Input formats: Google’s Gemini API image guide lists PNG, JPEG, WebP, HEIC, and HEIF. For inline image data, the total request—including prompt text, system instructions, and inline image bytes—is limited to 20 MB. Use the File API for larger files or images reused across requests. (Image understanding)
  • Quota: Limits depend on the model and project usage tier; Google applies rate limits per project, not per API key. View the project's active rate limits in AI Studio before setting production throughput. Do not assume one universal requests-per-minute or images-per-minute value. (Rate limits)
  • Region: The Gemini API and AI Studio are available only in listed countries and territories. Check Google’s current available-regions page for the location from which your team or application will access the service. (Available regions)
  • Cost context: Google’s August 26, 2025 launch announcement stated $30 per 1 million output tokens and 1,290 output tokens per image—about $0.039 per image at that announced rate. At that rate, 100 images calculate to $3.87 in output charges, before input charges. This is historical launch pricing, not confirmation of today’s applicable rate; verify current pricing for your account and API route before budgeting. (Google Developers Blog announcement)

Production decision: Use this route only if your project is eligible for the model, your image and full inline request fit the documented size limit (or you use the File API), and your region and project-specific quota meet the workload. The guide above covers the Gemini Developer API; do not assume its availability, limits, or lifecycle are identical on Vertex AI or another Google Cloud product.

Sources

Gemini 2.5 Flash Image model page — Gemini API

Image generation — Gemini API

Generating content — Gemini API

Image understanding — Gemini API

Gemini API deprecations

Rate limits — Gemini API

Available regions for Google AI Studio and Gemini API

Troubleshooting guide — Gemini API

OpenAI compatibility — Gemini API

Gemini 2.5 Flash Image notebook — Google Cloud

Introducing Gemini 2.5 Flash Image — Google Developers Blog

Gemini 2.5 Flash Image OpenAI-compatible endpoint report — Google AI Developers Forum

Contact us