Skip to main content
POST
/
v1beta
/
models
/
{model}
:generateContent
import os
from google import genai
from google.genai import types

client = genai.Client(
    http_options={"api_version": "v1beta", "base_url": "https://api.cometapi.com"},
    api_key=os.environ.get("COMETAPI_KEY"),
)

response = client.models.generate_content(
    model="gemini-3.1-flash-image-preview",
    contents="A Monarch butterfly anatomical sketch on textured parchment",
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
        image_config=types.ImageConfig(aspect_ratio="1:1", image_size="4K"),
    ),
)

final_image = None
for part in response.parts:
    if getattr(part, "thought", False):
        continue
    if part.text:
        print(part.text)
    elif image := part.as_image():
        final_image = image

if final_image:
    final_image.save("output.png")
{
  "candidates": [
    {
      "content": {
        "role": "<string>",
        "parts": [
          {
            "text": "<string>",
            "inlineData": {
              "mimeType": "<string>",
              "data": "<string>"
            },
            "thought": true,
            "thoughtSignature": "<string>"
          }
        ]
      },
      "index": 123,
      "safetyRatings": [
        {
          "category": "<string>",
          "probability": "<string>"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 123,
    "candidatesTokenCount": 123,
    "totalTokenCount": 123,
    "thoughtsTokenCount": 123
  }
}
For a short first request, start with the Gemini image API quickstart. For step-by-step examples, see Use Gemini image models.
Gemini image generation options can change as Google updates image models and generateContent. Check the Gemini image generation documentation for the latest complete parameter list and provider-specific behavior.
gemini-2.5-flash-image is scheduled for shutdown by the provider on 2026-10-02. For new projects, use gemini-3.1-flash-image-preview or gemini-3-pro-image-preview. See Google’s deprecation schedule for details.
Gemini image responses can include intermediate image parts where thought is true. These are not the final output. When saving generated images, skip thought: true parts and use the last image part where inlineData exists and thought is not true.

Authorizations

Authorization
string
header
required

Use your CometAPI API key.

Path Parameters

model
string
default:gemini-3.1-flash-image-preview
required

The Gemini image model to use. See the Models page for current options.

Body

application/json
contents
object[]
required

Conversation turns. Each item has a role ("user" or "model") and parts array containing text and/or image data.

generationConfig
object

Controls generation behavior — output modalities, image resolution, thinking, etc.

tools
object[]

Optional tools. Pass [{"google_search": {}}] to enable Google Search grounding for real-time information in generated images.

Response

200 - application/json

Success

candidates
object[]
usageMetadata
object