# gemini-3.1-flash-image ( nano banana 2 )

This document describes the input and output parameters for calling the `gemini-3.1-flash-image-preview` model (Model ID: `gemini-3.1-flash-image-preview`) API, for your reference when using the interface.

---

Only a subset of fields is shown below. For full details, please refer to the official documentation: [New Image Capabilities in Gemini 3 Pro](https://ai.google.dev/gemini-api/docs/image-generation?hl=zh-en#gemini-3-capabilities)

## Request Parameters

### Request Body

| Field Name                               | Type   | Required | Default           | Description                                                                                 |
| ---------------------------------------- | ------ | -------- | ----------------- | ------------------------------------------------------------------------------------------- |
| contents                                 | array  | Yes      | -                 | The content of the request, consisting of one or more parts.                                |
| contents.role                            | string | Yes      | "user"            | The role of the content. Must be "user".                                                     |
| contents.parts                           | array  | Yes      | -                 | The detailed parts of the content.                                                           |
| contents.parts.text                      | string | Optional | -                 | The prompt text.                                                                            |
| generationConfig                         | object | Optional | -                 | Generation configuration.                                                                   |
| generationConfig.responseModalities      | array  | Optional | ["TEXT", "IMAGE"] | Expected response formats, can include text and/or image.                                   |
| generationConfig.imageConfig             | object | Optional | -                 | Image generation configuration.                                                             |
| generationConfig.imageConfig.aspectRatio | string | Optional | "1:1"             | Image aspect ratio. Supported values: "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9". |
| generationConfig.imageConfig.imageSize   | string | Optional | "1K"              | Image resolution. Supported values: `"512"` (0.5K), `"1K"`, `"2K"`, `"4K"`. Note: `512` has no K suffix.  |

## Response Parameters

| Field Name                                     | Type     | Description                                  |
| ---------------------------------------------- | -------- | -------------------------------------------- |
| candidates                                     | `array`  | List of returned candidate contents.         |
| candidates.content                             | `object` | Candidate content.                           |
| candidates.content.parts                       | `array`  | Content parts, may include text and images.  |
| candidates.content.parts.text                  | `string` | Text description returned by the model.      |
| candidates.content.parts.inlineData            | `object` | Inline image data.                           |
| candidates.content.parts.inlineData.data       | `string` | Base64-encoded image data.                   |
| candidates.content.parts.inlineData.mimeType   | `string` | MIME type of the data, e.g., "image/png".    |
| candidates.finishReason                        | `string` | Reason for completion, e.g., "STOP".         |
| usageMetadata                                  | `object` | Metadata for token usage.                    |
| error                                          | `Object` | Error information object.                    |

## Example

### Gemini-Compatible API

`POST https://api-us-ca.umodelverse.ai/v1beta/models/gemini-3.1-flash-image-preview:generateContent`

#### Image Generation (Text-to-Image)

> ⚠️ Note: You must include `responseModalities: ["TEXT", "IMAGE"]` in the configuration.

<!-- tabs:start -->

##### ** curl **

```bash
curl -s -X POST \
  "https://api-us-ca.umodelverse.ai/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
  -H "x-goog-api-key: $MODELVERSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "Da Vinci style anatomical sketch of a dissected Monarch butterfly. Detailed drawings of the head, wings, and legs on textured parchment with notes in English."}]}],
    "tools": [{"google_search": {}}],
  "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
    }
  }' | jq -r '.candidates[0].content.parts[] | select(.inlineData and (.thought | not)) | .inlineData.data' | head -1 | base64 --decode > butterfly.png
```

##### ** python **

```python
from google import genai
from google.genai import types
import os

client = genai.Client(
    api_key=os.getenv("MODELVERSE_API_KEY", "<MODELVERSE_API_KEY>"),  # Your API key
    http_options=types.HttpOptions(
        base_url="https://api-us-ca.umodelverse.ai"
    ),
)

prompt = "Da Vinci style anatomical sketch of a dissected Monarch butterfly. Detailed drawings of the head, wings, and legs on textured parchment with notes in English."
aspect_ratio = "1:1"  # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"
resolution = "1K"  # "512", "1K", "2K", "4K" (note: use "512" without K suffix for 0.5K)

response = client.models.generate_content(
    model="gemini-3.1-flash-image-preview",
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
        image_config=types.ImageConfig(
            aspect_ratio=aspect_ratio, image_size=resolution
        ),
    ),
)

for part in response.parts:
    # Skip intermediate images generated during the thinking process, keep only final output
    if getattr(part, "thought", False):
        continue
    if part.text is not None:
        print(part.text)
    elif image := part.as_image():
        image.save("butterfly.png")

```

<!-- tabs:end -->

### Response Example

```json
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Here is the anatomical sketch of a dissected Monarch butterfly in Da Vinci style..."
          },
          {
            "inlineData": {
              "data": "iVBORw0KGgoAAAANSUhEUgAA...",
              "mimeType": "image/png"
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "candidatesTokenCount": 1315,
    "totalTokenCount": 1331
  }
}
```

```json
{
  "error": {
    "message": "error_message",
    "type": "error_type",
    "param": "request_id",
    "code": "error_code"
  }
}
```
