Skip to main content
POST
Generate an image (native Gemini, SSE streaming)
Use the native Gemini streamGenerateContent endpoint to receive results in chunks. For image models, this endpoint is supported only for Google/Gemini image models. Use the appropriate OpenAI image endpoint for other image models. Native streaming for Gemini chat models is outside the scope of this image reference.

Features

  • Use the native Gemini streamGenerateContent endpoint to receive image results as SSE frames
  • Use the same request body as the non-streaming endpoint for text-to-image, image editing, multiple references, or multi-turn editing
  • Configure image output with generationConfig.responseModalities and generationConfig.imageConfig; no stream: true field is needed
  • Receive text and Base64 images in candidates[].content.parts[], not OpenAI image URLs or named events

Authentication

POST https://api.hairoute.ai/v1/models/{model}:streamGenerateContent {model} is an available Google/Gemini image model from the model list, not a request-body field. Use your HaiRoute API key in x-goog-api-key: YOUR_API_KEY or Authorization: Bearer YOUR_API_KEY. The body can include contents, generationConfig.responseModalities, and optionally generationConfig.imageConfig; this route always responds with text/event-stream.

Supported image models

This endpoint is only for Google/Gemini image models.

Quick example

Generation modes

These examples show request bodies only. All four modes use this page’s endpoint and authentication; no separate mode parameter is needed. Replace every Base64 placeholder with actual raw Base64 image data.

Text-to-image

Send a text prompt without an input image; the Quick example above is ready to adapt. Local changes and style adjustments are prompt instructions, not separate API parameters.

Single-image editing

Put one image and an editing instruction in the same user message. This example replaces the background; you can also ask to preserve the subject, change an element, or adjust the style.

Multiple-reference composition

Put multiple reference images in one user message’s parts and describe the role of each image in the text. Input count, size limits, and results depend on the selected model and channel.

Multi-turn editing

Send the previous user request, the actual previous candidates[0].content as a model message, and your new instruction in that order within contents. This body illustrates the structure only:
For a real request, do not reconstruct or trim the prior model content. If the prior response was streamed, collect content.parts for the same candidate from all data: frames in order, then replay the complete content as the model message, preserving images, text, and thoughtSignature when present. The last SSE frame alone is not enough. Multiple references and multi-turn results depend on the selected image model.

Streaming response

The server sends SSE frames. Each data: line contains one complete Gemini GenerateContentResponse JSON chunk. The following frames are illustrative; the Base64 string is a placeholder:
Parse each SSE data frame and inspect candidates[].content.parts[]: text contains text; inlineData.data contains a Base64 image and inlineData.mimeType identifies its format. Some frames may omit these fields; usage usually arrives in a later chunk. This stream has no OpenAI-style named events or [DONE] marker. Do not treat a frame or Base64 image as an image URL.

Troubleshooting

Check the HTTP status and the error in the response body first; for streaming requests, inspect every received SSE data: frame. Never paste API keys or complete image Base64 in logs or support requests.

Next steps

请求头

x-goog-api-key
string
必填

HaiRoute API key for the native request. Alternatively, use Authorization: Bearer YOUR_API_KEY instead (choose one).

路径参数

model
string
必填

An available Google/Gemini image model in HaiRoute

请求体

application/json
contents
object[]
必填

Required native Gemini messages. Use a user text part for text-to-image; add inlineData for image-to-image.

Minimum array length: 1
systemInstruction
object
generationConfig
object

Native Gemini generation settings; availability depends on the image model.

响应

SSE data stream: each data frame contains a Gemini GenerateContentResponse JSON chunk; no named events or [DONE].

The response is of type string.