GuideImage GenerationNano banana

OpenAI Chat Format

Use the OpenAI Chat Completions format to trigger Gemini image generation, with extra_body config for aspect ratio and size, and tips for parsing Base64 images.

OpenAI Chat Format (OpenAI Chat Format / Gemini Image)

This endpoint wraps the request in the standard OpenAI Chat Completions structure, letting you trigger Gemini's image generation capability through chat instructions.


📌 Basics

  • Endpoint: POST /v1/chat/completions
  • Authentication: Bearer Token
  • Content type: application/json

📥 Request Parameters (Request Body)

ParameterTypeRequiredDescription
modelstringYesModel name, e.g. gemini-3-pro-image-preview.
messagesarrayYesChat context list.
└─ rolestringYesRole (user, assistant, system).
└─ contentstringYesPrompt content, e.g. "draw a cat".
streambooleanYesWhether to enable streaming output (recommended false for image generation).
extra_bodyobjectNoExtension parameters (for Gemini-specific features).
└─ googleobjectNoGoogle official configuration extension.
└─ └─ image_configobjectNoImage generation configuration.
└─ └─ └─ aspect_ratiostringNoAspect ratio, e.g. 16:9, 1:1.
└─ └─ └─ image_sizestringNoQuality level, e.g. 2K, 4K.

📤 Response

Status code: 200 OK

FieldTypeDescription
idstringUnique request ID.
objectstringObject type, always chat.completion.
choicesarrayList of generation results.
└─ messageobjectContains role and content (images are usually returned as Markdown Base64).
└─ finish_reasonstringFinish reason, e.g. stop.
usageobjectToken usage statistics.

📝 Request Example

{
  "model": "gemini-3-pro-image-preview",
  "stream": false,
  "messages": [
    {
      "role": "user",
      "content": "draw a cat"
    }
  ],
  "extra_body": {
    "google": {
      "image_config": {
        "aspect_ratio": "16:9",
        "image_size": "2K"
      }
    }
  }
}

📝 Response Example

{
  "id": "chatcmpl-20251211...",
  "model": "gemini-3-pro-image-preview",
  "object": "chat.completion",
  "created": 1765440499,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "![image](data:image/jpeg;base64,/9j/4AAQSk...)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 4,
    "completion_tokens": 1359,
    "total_tokens": 1363
  }
}

💡 Developer Tips

  1. Extracting the image: The image in the response is usually embedded in content as Markdown, ![image](data:image/jpeg;base64,...). Clients should extract the Base64 part for display.
  2. Parameter compatibility: extra_body is the standard OpenAI SDK extension field for passing configuration specific to downstream models; it does not break the standard OpenAI protocol structure.
  3. Mock testing: For local debugging you can use the mock address http://127.0.0.1:4523/mock/7707339.

On this page