---
title: "Image Generation (Streaming)"
method: POST
path: "/v1beta/models/gemini-3.1-flash-image:streamGenerateContent"
---

# Image Generation (Streaming)

`POST /v1beta/models/gemini-3.1-flash-image:streamGenerateContent`

Generate images using Gemini 3.1 Flash Image in streaming mode via SSE. The endpoint returns newline-delimited Server-Sent Events. Thinking chunks (thought=true) arrive first, followed by the image chunk containing inlineData, and finally a usage metadata chunk.

## Query parameters

- `key` string, required
- `alt` 'sse'

## Request body

- object
  - `contents` object[], required — Array of conversation turns. Each turn has a role and parts. A part can be a text prompt, or an inline_data image (base64). To use a reference image, include both a text part and an inline_data part in the same parts array.
    - `role` 'user' | 'model'
    - `parts` object[] — List of content parts. Combine text and inline_data in the same array to send a prompt alongside a reference image.
      - `text` string — Text prompt
      - `inline_data` object — Reference image encoded as base64 (snake_case in request body). Note: in the streaming response, the field is inlineData (camelCase).
        - `mime_type` 'image/jpeg' | 'image/png' | 'image/webp' — MIME type of the image. Supported: image/jpeg, image/png, image/webp
        - `data` string — Base64-encoded image data.
  - `generationConfig` object, required
    - `responseModalities` string[], required — Output modalities. Use ["IMAGE"] for image-only output, or ["TEXT", "IMAGE"] to receive both a caption and the image.
    - `imageConfig` object — Image generation configuration
      - `aspectRatio` '1:1' | '4:3' | '3:4' | '16:9' | '9:16' — Desired aspect ratio of the generated image.
      - `imageSize` '1K' | '2K' | '4K' — Resolution of the generated image. 1K ≈ 1024px, 2K ≈ 2048px, 4K ≈ 4096px on the long edge.

## Response `200`

Streaming SSE response. Each line starts with "data:" followed by a JSON chunk. Three chunk types are delivered in order: (1) Thinking chunks — parts[0].thought is true; (2) Image chunk — parts[0].inlineData contains mimeType and base64 data (camelCase); (3) Final usage chunk — top-level usageMetadata with thoughtsTokenCount.

- StreamChunk — A single SSE chunk. Three variants are possible: thinking chunk (parts[].thought=true), image chunk (parts[].inlineData), or usage chunk (no candidates).
  - `candidates` object[] — Present in thinking and image chunks; absent in the final usage chunk.
    - `content` object
      - `role` string
      - `parts` object[]
        - `text` string — Thinking text (present in thinking chunks)
        - `thought` boolean — True when this part is an internal thinking chunk
        - `inlineData` object — Generated image data (camelCase; present in image chunk only). Note: the request body uses inline_data (snake_case).
          - `mimeType` string
          - `data` string — Base64-encoded generated image
  - `usageMetadata` object — Token usage. Final chunk contains full details including thoughtsTokenCount.
    - `promptTokenCount` integer
    - `candidatesTokenCount` integer
    - `totalTokenCount` integer
    - `thoughtsTokenCount` integer — Number of tokens used for internal thinking (present in final usage chunk)
    - `trafficType` string
    - `promptTokensDetails` object[]
      - `modality` string
      - `tokenCount` integer
    - `candidatesTokensDetails` object[]
      - `modality` string
      - `tokenCount` integer
  - `modelVersion` string
  - `createTime` string
  - `responseId` string

## Other responses

- `400` — Invalid request
- `401` — Unauthorized
- `429` — Rate limit exceeded

---

[API](https://skmtc.net/anyfast/apis/anthropic-claude-compatible-endpoint.md) · [All operations](https://skmtc.net/anyfast/apis/anthropic-claude-compatible-endpoint/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/anyfast/anthropic-claude-compatible-endpoint/versions/edfc53e182a1/schema)
