> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyfast.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini Nano Banana 2.1 (Stream)

> Generate images with Gemini Nano Banana 2.1 and receive response chunks through the Gemini API streaming endpoint.

Gemini Nano Banana 2.1 can return generated content through the native Gemini `streamGenerateContent` endpoint. The endpoint delivers Server-Sent Events (SSE), allowing your application to process response chunks as they arrive.

## Key capabilities

* **SSE streaming** — Receive `GenerateContentResponse` chunks as they are generated.
* **Text-to-image** — Generate images from text prompts.
* **Image editing** — Include a reference image and an editing instruction in the same request.
* **Image settings** — Set output aspect ratio and resolution with `imageConfig`.

## SSE response format

The endpoint returns SSE events. Each `data:` line contains a JSON `GenerateContentResponse` chunk. Read `candidates[].content.parts[]` from each chunk; a part can contain text or image data. Image data uses `inlineData` with a MIME type and Base64-encoded `data`. A chunk may also contain usage metadata. The number and ordering of chunks can vary by response.

```text theme={null}
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"..."}]}}]}

data: {"candidates":[{"content":{"role":"model","parts":[{"inlineData":{"mimeType":"image/png","data":"<base64>"}}]}}]}

data: {"usageMetadata":{"promptTokenCount":24,"candidatesTokenCount":1024,"totalTokenCount":1048}}
```

> [!NOTE]
>
> Request fields use the native Gemini API format. In a streaming response, generated image bytes are returned in `inlineData.data` using Base64 encoding.

## Text-to-image example

```bash cURL theme={null}
curl --no-buffer \
  "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:streamGenerateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [{
        "text": "Create a minimal isometric illustration of a green data center."
      }]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
    }
  }'
```

```python Python theme={null}
import base64
import json
import requests

url = "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:streamGenerateContent"
response = requests.post(
    url,
    params={"key": "YOUR_API_KEY"},
    json={
        "contents": [{
            "role": "user",
            "parts": [{"text": "Create a minimal isometric illustration of a green data center."}],
        }],
        "generationConfig": {
            "responseModalities": ["TEXT", "IMAGE"],
            "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"},
        },
    },
    stream=True,
)
response.raise_for_status()

for line in response.iter_lines():
    if not line or not line.startswith(b"data:"):
        continue
    chunk = json.loads(line[len(b"data:"):].strip())
    for candidate in chunk.get("candidates", []):
        for part in candidate.get("content", {}).get("parts", []):
            if part.get("thought"):
                continue
            if "text" in part:
                print(part["text"], end="")
            if "inlineData" in part:
                image = base64.b64decode(part["inlineData"]["data"])
                with open("output.png", "wb") as image_file:
                    image_file.write(image)
                print("\nImage saved to output.png")
```

## Image editing example

Send the editing instruction and the reference image in the same `parts` array. The request uses `inlineData` with Base64 image bytes.

```bash cURL theme={null}
curl --no-buffer \
  "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:streamGenerateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "role": "user",
      "parts": [
        {"text": "Keep the product unchanged, replace the background with a warm wooden desk."},
        {"inlineData": {"mimeType": "image/png", "data": "YOUR_BASE64_IMAGE_DATA"}}
      ]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {"aspectRatio": "4:3", "imageSize": "2K"}
    }
  }'
```

## Parameters

| Parameter | Type | Required | Description |
| - | - | -: | - |
| `key` | string | Yes | AnyFast API key, passed as a query parameter. |
| `alt` | string | No | Set to `sse` to explicitly request SSE mode. |
| `contents` | array | Yes | Conversation turns with text and optional reference media. |
| `generationConfig.responseModalities` | array | No | Select `IMAGE` or `TEXT` and `IMAGE`. |
| `generationConfig.imageConfig.aspectRatio` | string | No | Output aspect ratio, such as `1:1`, `4:3`, or `16:9`. |
| `generationConfig.imageConfig.imageSize` | string | No | Output size: `1K`, `2K`, or `4K`. |

> **[API Reference](https://docs.anyfast.ai/api-reference/model-api/google/gemini-nano-banana-2-1-stream.md)**
>
> View the streaming request and response schema for Gemini Nano Banana 2.1.

## Official references

* [Gemini Nano Banana 2.1 model page](https://ai.google.dev/gemini-api/docs/models/gemini-nano-banana-2.1)
* [Google image generation guide](https://ai.google.dev/gemini-api/docs/generate-content/image-generation)
* [Google streaming content generation guide](https://ai.google.dev/gemini-api/docs/generate-content/text-generation)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.