> ## 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 3.1 Flash Image

> Google's Gemini 3.1 Flash Image for AI image generation and editing via Gemini API.

Gemini 3.1 Flash Image is Google's image generation model, available through Anyfast via the native Gemini API. It can generate images from text prompts and edit existing images using a reference photo.

<Info>
  Google released `gemini-3.1-flash-image` as the GA model for Nano Banana 2 on May 28, 2026. Use this stable model ID instead of `gemini-3.1-flash-image-preview`, which is deprecated and scheduled to shut down on June 25, 2026.
</Info>

## Key capabilities

* **Text-to-image** — Generate images from text descriptions
* **Image editing** — Pass a reference image in `inline_data` alongside your text instruction
* **Aspect ratio control** — `1:1`, `4:3`, `3:4`, `16:9`, `9:16`
* **Resolution control** — `512` (512px), `1K` (\~1024px), `2K` (\~2048px), `4K` (\~4096px) on the long edge
* **Multi-modal output** — Return both the image and a text caption with `responseModalities: ["TEXT", "IMAGE"]`

> **Note:** If you need the generated image returned as a URL, select the **Aggregate-NanoUrl** group when creating your API token.

## Text-to-image example

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image:generateContent?key=YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "role": "user",
          "parts": [
            { "text": "Generate an image of a sunset over mountains" }
          ]
        }
      ],
      "generationConfig": {
        "responseModalities": ["TEXT", "IMAGE"],
        "imageConfig": {
          "aspectRatio": "16:9",
          "imageSize": "1K"
        }
      }
    }'
  ```

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

  url = "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image:generateContent"
  response = requests.post(url, params={"key": "YOUR_API_KEY"}, json={
      "contents": [
          {
              "role": "user",
              "parts": [{"text": "Generate an image of a sunset over mountains"}]
          }
      ],
      "generationConfig": {
          "responseModalities": ["TEXT", "IMAGE"],
          "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
      }
  })

  for part in response.json()["candidates"][0]["content"]["parts"]:
      if "inline_data" in part:
          with open("output.jpg", "wb") as f:
              f.write(base64.b64decode(part["inline_data"]["data"]))
          print("Image saved to output.jpg")
      elif "text" in part:
          print("Caption:", part["text"])
  ```

  ```python GenAI SDK theme={null}
  import google.genai as genai

  client = genai.Client(api_key="YOUR_API_KEY")

  response = client.models.generate_content(
      model="gemini-3.1-flash-image",
      contents="Generate an image of a sunset over mountains",
      config={
          "responseModalities": ["TEXT", "IMAGE"],
          "imageConfig": {
              "aspectRatio": "16:9",
              "imageSize": "1K"
          }
      }
  )

  for candidate in response.candidates:
      for part in candidate.content.parts:
          if part.inline_data:
              with open("output.jpg", "wb") as f:
                  f.write(part.inline_data.data)
              print("Image saved to output.jpg")
          elif part.text:
              print("Caption:", part.text)
  ```
</CodeGroup>

## Image editing example (with reference image)

Include both a `text` instruction and an `inline_data` reference image in the same `parts` array.

<CodeGroup>
  ```bash cURL theme={null}
  # First encode your image to base64:
  # BASE64=$(base64 -i your_photo.jpg)
  #
  # Then send the request:
  curl "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image:generateContent?key=YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "role": "user",
          "parts": [
            {
              "text": "Hi, this is a picture of me. Can you add a llama next to me?"
            },
            {
              "inline_data": {
                "mime_type": "image/jpeg",
                "data": "<YOUR_BASE64_ENCODED_IMAGE>"
              }
            }
          ]
        }
      ],
      "generationConfig": {
        "responseModalities": ["TEXT", "IMAGE"],
        "imageConfig": {
          "aspectRatio": "1:1",
          "imageSize": "1K"
        }
      }
    }'
  ```

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

  # Read and encode your reference image
  with open("your_photo.jpg", "rb") as f:
      image_b64 = base64.b64encode(f.read()).decode("utf-8")

  url = "https://www.anyfast.ai/v1beta/models/gemini-3.1-flash-image:generateContent"
  response = requests.post(url, params={"key": "YOUR_API_KEY"}, json={
      "contents": [
          {
              "role": "user",
              "parts": [
                  {
                      "text": "Hi, this is a picture of me. Can you add a llama next to me?"
                  },
                  {
                      "inline_data": {
                          "mime_type": "image/jpeg",
                          "data": image_b64          # ← paste your base64 string here
                      }
                  }
              ]
          }
      ],
      "generationConfig": {
          "responseModalities": ["TEXT", "IMAGE"],
          "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
      }
  })

  for part in response.json()["candidates"][0]["content"]["parts"]:
      if "inline_data" in part:
          with open("output.jpg", "wb") as f:
              f.write(base64.b64decode(part["inline_data"]["data"]))
          print("Image saved to output.jpg")
      elif "text" in part:
          print("Caption:", part["text"])
  ```

  ```python GenAI SDK theme={null}
  import google.genai as genai

  client = genai.Client(api_key="YOUR_API_KEY")

  # Read and encode your reference image
  with open("your_photo.jpg", "rb") as f:
      image_bytes = f.read()

  response = client.models.generate_content(
      model="gemini-3.1-flash-image",
      contents=[
          "Hi, this is a picture of me. Can you add a llama next to me?",
          genai.types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")
      ],
      config={
          "responseModalities": ["TEXT", "IMAGE"],
          "imageConfig": {
              "aspectRatio": "1:1",
              "imageSize": "1K"
          }
      }
  )

  for candidate in response.candidates:
      for part in candidate.content.parts:
          if part.inline_data:
              with open("output.jpg", "wb") as f:
                  f.write(part.inline_data.data)
              print("Image saved to output.jpg")
          elif part.text:
              print("Caption:", part.text)
  ```
</CodeGroup>

## Parameters

| Parameter                                  | Type   | Required | Description                                                   |
| ------------------------------------------ | ------ | -------- | ------------------------------------------------------------- |
| `key`                                      | string | Yes      | API key (query parameter)                                     |
| `contents[].parts[].text`                  | string | Yes      | Text prompt or instruction                                    |
| `contents[].parts[].inline_data.mime_type` | string | No       | Reference image type: `image/jpeg`, `image/png`, `image/webp` |
| `contents[].parts[].inline_data.data`      | string | No       | Base64-encoded reference image                                |
| `generationConfig.responseModalities`      | array  | Yes      | `["IMAGE"]` or `["TEXT", "IMAGE"]`                            |
| `generationConfig.imageConfig.aspectRatio` | string | No       | `1:1` / `4:3` / `3:4` / `16:9` / `9:16`                       |
| `generationConfig.imageConfig.imageSize`   | string | No       | `512` / `1K` / `2K` / `4K` (default: `1K`)                    |

<Card title="API Reference" icon="code" href="/api-reference/model-api/google/gemini-3.1-flash-image">
  View the interactive API playground for Gemini 3.1 Flash Image.
</Card>

<script src="/feedback.js" />
