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

# Gemini 3.1 Flash Image Preview

> Call Google Gemini 3.1 Flash Image Preview via Gemini API for AI image generation and editing.

Gemini 3.1 Flash Image Preview is Google's image generation model, available through Starrise AI via the native Gemini API. It supports text-to-image generation and image editing with reference images.

## Key Capabilities

* **Text-to-image** — Generate images from text descriptions
* **Image editing** — Pass a reference image via `inline_data` combined with text instructions for editing
* **Aspect ratio control** — `1:1`, `4:3`, `3:4`, `16:9`, `9:16`
* **Resolution control** — `512` (512px), `1K` (\~1024px), `2K` (\~2048px), `4K` (\~4096px, by longest side)
* **Multimodal output** — Return both image and text description via `responseModalities: ["TEXT", "IMAGE"]`

## Text-to-Image Example

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

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

  url = "https://ai.alad.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
  response = requests.post(url, params={"key": "YOUR_API_KEY"}, json={
      "contents": [
          {
              "role": "user",
              "parts": [{"text": "Generate an image of a mountain sunset"}]
          }
      ],
      "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-preview",
      contents="Generate an image of a mountain sunset",
      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)

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

<CodeGroup>
  ```bash cURL theme={null}
  # First convert image to base64:
  # BASE64=$(base64 -i your_photo.jpg)
  #
  # Then send the request:
  curl "https://ai.alad.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent?key=YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [
        {
          "role": "user",
          "parts": [
            {
              "text": "This is a photo of me, please add an alpaca beside 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 the reference image
  with open("your_photo.jpg", "rb") as f:
      image_b64 = base64.b64encode(f.read()).decode("utf-8")

  url = "https://ai.alad.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent"
  response = requests.post(url, params={"key": "YOUR_API_KEY"}, json={
      "contents": [
          {
              "role": "user",
              "parts": [
                  {
                      "text": "This is a photo of me, please add an alpaca beside me"
                  },
                  {
                      "inline_data": {
                          "mime_type": "image/jpeg",
                          "data": image_b64          # ← paste 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
  from google.genai import types

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

  # Read the image
  with open("your_photo.jpg", "rb") as f:
      image_bytes = f.read()

  response = client.models.generate_content(
      model="gemini-3.1-flash-image-preview",
      contents=[
          "This is a photo of me, please add an alpaca beside me",
          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 data                           |
| `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="/en/api-reference/model-api/google/gemini-3.1-flash-image-preview">
  View the interactive API Playground for Gemini 3.1 Flash Image Preview.
</Card>
