> For clean Markdown of any page, append `.md` to the page URL.
> For a complete documentation index, see https://docs.sarvam.ai/llms.txt.
> For full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# Gemma 4 31B

> Gemma 4 31B is a 31B-parameter instruction-tuned model for text and image understanding. It supports a 131,072-token context window and tool calling.

Gemma 4 31B is a 31B-parameter instruction-tuned model for text and image
understanding. It accepts text and inline images with a **131,072-token context
window**.

Gemma 4 31B is available in **beta** and is not specifically tuned for Indian
languages. Access is granted per API key —
[contact us](/api/getting-started/help) to request access.

## At a glance

|                       |                                                                                                                                    |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| **Model ID**          | `gemma4`                                                                                                                           |
| **Best for**          | Image captioning, classification, and visual question answering                                                                    |
| **Context window**    | 131,072 tokens                                                                                                                     |
| **Input → output**    | Text and images → text                                                                                                             |
| **Reasoning**         | No; answers are returned directly                                                                                                  |
| **Tool calling**      | Supported                                                                                                                          |
| **Log probabilities** | Supported on [Chat Completion V2](/api-reference/chat/chat-completions-v2) (`logprobs` / `top_logprobs`)                           |
| **APIs**              | [`POST /v2/chat/completions`](/api-reference/chat/chat-completions-v2) and [`POST /v2/responses`](/api-reference/responses/create) |
| **Pricing**           | ₹36.6 input · ₹13.73 cached input · ₹91.5 output per 1M tokens ([Pricing](/api/getting-started/pricing))                           |

## Key capabilities

#### Image understanding

Caption, classify, and answer questions about images supplied as base64 data URIs.

#### Direct answers

Receive the answer without a separate reasoning pass.

#### Tool calling

Use OpenAI-compatible `tools` and `tool_choice` for agentic workflows.

#### Streaming

Receive answer deltas as server-sent events.

## Quickstart

The examples send a local image as a base64 data URI. Remote image URLs are not
supported.

#### Python

```python
import base64

from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

with open("chart.png", "rb") as image:
    encoded = base64.b64encode(image.read()).decode("utf-8")

response = client.chat.completions_v2(
    model="gemma4",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What trend does this chart show?"},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/png;base64,{encoded}"},
                },
            ],
        }
    ],
    temperature=1,
    top_p=0.95,
    max_tokens=500,
)

print(response.choices[0].message.content)
```

#### JavaScript

```javascript
import { readFileSync } from "fs";
import { SarvamAIClient } from "sarvamai";

const client = new SarvamAIClient({
    apiSubscriptionKey: "YOUR_SARVAM_API_KEY",
});

async function main() {
    const encoded = readFileSync("chart.png").toString("base64");

    const response = await client.chat.completionsV2({
        model: "gemma4",
        messages: [
            {
                role: "user",
                content: [
                    { type: "text", text: "What trend does this chart show?" },
                    {
                        type: "image_url",
                        image_url: {
                            url: `data:image/png;base64,${encoded}`,
                        },
                    },
                ],
            },
        ],
        temperature: 1,
        top_p: 0.95,
        max_tokens: 500,
    });

    console.log(response.choices[0].message.content);
}

main();
```

#### cURL

```bash
IMAGE_BASE64=$(base64 < chart.png | tr -d '\n')

curl -X POST https://api.sarvam.ai/v2/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "model": "gemma4",
  "messages": [
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What trend does this chart show?"},
        {
          "type": "image_url",
          "image_url": {
            "url": "data:image/png;base64,$IMAGE_BASE64"
          }
        }
      ]
    }
  ],
  "temperature": 1,
  "top_p": 0.95,
  "max_tokens": 500
}
JSON
```

## API reference

Call [`POST /v2/chat/completions`](/api-reference/chat/chat-completions-v2) or
[`POST /v2/responses`](/api-reference/responses/create) with `model: "gemma4"`. Request
fields (`logprobs`, `stream`, tools, image parts, and the rest) are defined in those API
references — not duplicated on this model page.

Images must be **base64 data URIs** in `image_url` content parts (remote URLs are rejected).

## Limits and errors

| Area              | Limit or condition                                                  | API response                                               | Recommended action                                                                              |
| ----------------- | ------------------------------------------------------------------- | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| Context           | Prompt tokens plus `max_tokens` exceed 131,072                      | `422 unprocessable_entity_error`                           | Shorten the prompt, send fewer images, or reduce `max_tokens`.                                  |
| Output            | `max_tokens` exceeds 131,072                                        | `400 invalid_request_error`                                | Set `max_tokens` within the context window.                                                     |
| Request size      | Request body exceeds 10 MB                                          | `413 invalid_request_error`                                | Reduce the request body; base64-encoded images count toward the limit.                          |
| Image URL         | An `image_url` uses an `http(s)` URL                                | `400 invalid_request_error`                                | Inline the image as a base64 data URI.                                                          |
| Structured output | `response_format` is combined with a JSON instruction in the prompt | `200` with whitespace output and `finish_reason: "length"` | Remove the duplicate JSON instruction from the prompt.                                          |
| Tools             | The tool schema is invalid                                          | `400 invalid_request_error`                                | Check the schema requirements in [Chat Completion V2](/api-reference/chat/chat-completions-v2). |
| Authentication    | API key is missing or invalid                                       | `403 invalid_api_key_error`                                | Check the `api-subscription-key` header.                                                        |
| Access            | API key does not have beta access                                   | `400 invalid_request_error`                                | [Request beta access](/api-reference/beta-apis).                                                |
| Model             | Model ID is unknown or unavailable                                  | `404 not_found_error`                                      | Confirm the ID with [`GET /v2/models`](/api-reference/chat/models-v2).                          |
| Quota             | No credits remain                                                   | `402 insufficient_quota_error`                             | Add credits in the [dashboard](https://dashboard.sarvam.ai).                                    |
| Rate limit        | Request or concurrency limit is exceeded                            | `429 rate_limit_exceeded_error`                            | Back off and retry.                                                                             |
| Availability      | Model remains overloaded after retries                              | `503 model_overloaded`                                     | Retry later.                                                                                    |