> 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 an open-source instruction-tuned model served on Sarvam infrastructure. 128K context window with image input and tool calling.

Gemma 4 31B is an [open-source model](/api/getting-started/models/open-source) served by
Sarvam in **beta**, and is **not tuned for Indian languages**. Beta access is granted per
API key — [contact us](/api/getting-started/help) to request whitelisting. For Indian
language workloads use [Sarvam-105B](/api/getting-started/models/sarvam-105b); for document
and image understanding in Indian languages, use
[Sarvam Vision](/api/getting-started/models/sarvam-vision).

A 31B-parameter instruction-tuned model. Its distinguishing feature on Sarvam is
**image input** — it is the only chat model here that accepts images alongside text in the
chat completions format.

## At a Glance

|                  |                                                                                                                                                                                                                                                                   |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Model ID**     | `gemma4`                                                                                                                                                                                                                                                          |
| **What it does** | Chat LLM with image understanding                                                                                                                                                                                                                                 |
| **Languages**    | Not tuned for Indian languages by Sarvam                                                                                                                                                                                                                          |
| **APIs**         | [Open-Source Models API Reference](/api-reference/open-source/chat-completions) (`POST /v2/chat/completions`, OpenAI-compatible, streaming supported) — also see [using an open-source model](/api/getting-started/models/open-source#using-an-open-source-model) |
| **Input limits** | 131,072-token context window; images as base64 data URIs only; 10 MB request cap — [all limits](#limits-and-errors)                                                                                                                                               |
| **Benchmarks**   | None published by Sarvam                                                                                                                                                                                                                                          |
| **Pricing**      | ₹36.6 input · ₹13.73 cached input · ₹91.5 output — per 1M tokens ([Pricing](/api/getting-started/pricing))                                                                                                                                                        |
| **Best for**     | Image captioning, classification, and visual question answering                                                                                                                                                                                                   |
| **Not for**      | Indian language workloads or document OCR — use [Sarvam-105B](/api/getting-started/models/sarvam-105b) or [Sarvam Vision](/api/getting-started/models/sarvam-vision)                                                                                              |

## Why you might use it

#### Image understanding

Accepts images inline as base64 data URIs in the standard `image_url` content part, so
you can caption, classify, or answer questions about images.

#### Direct answers

Not a reasoning model — it replies without a chain-of-thought pass, so the whole
token budget goes to the answer.

## Model Specifications

#### Key Considerations

<ul>
  <li>
    Model ID: 

    <code>gemma4</code>
  </li>

  <li>
    Context window: 131,072 tokens (128K)
  </li>

  <li>
    Image input: supported — base64 data URIs only
  </li>

  <li>
    Tool calling: supported
  </li>

  <li>
    Reasoning: none — answers directly, never returns 

    <code>reasoning_content</code>

    , and 

    <code>reasoning_effort</code>

     has no effect
  </li>

  <li>
    Maximum request size: 10 MB (inclusive of encoded images)
  </li>

  <li>
    Temperature range: 0 to 2 (unset uses the model's own default)
  </li>

  <li>
    Top-p range: greater than 0, up to 1
  </li>

  <li>
    Supports streaming and non-streaming responses
  </li>

  <li>
    OpenAI-compatible chat completions format
  </li>
</ul>

Each image is estimated at roughly **1,024 tokens** when Sarvam checks your request
against the context window. Budget for this when sending several images with a long prompt.

## Gemma 4 31B vs other options

| Feature                    | Gemma 4 31B        | GLM-5.2                 | Sarvam-105B                         | Sarvam Vision             |
| -------------------------- | ------------------ | ----------------------- | ----------------------------------- | ------------------------- |
| **Context window**         | 131,072            | 524,288                 | 128,000                             | —                         |
| **Image input**            | ✅                  | ❌                       | ❌                                   | ✅ (documents)             |
| **Tool calling**           | ✅                  | ✅                       | ✅                                   | —                         |
| **Indian language tuning** | None               | None                    | 23 languages                        | 23 languages              |
| **Best for**               | Image + text tasks | Very long-context tasks | Indian-language chat & agentic work | Document OCR & extraction |

For **document** understanding — scanned forms, invoices, PDFs, especially in Indian
languages — use [Sarvam Vision](/api/getting-started/models/sarvam-vision),
which is purpose-built for OCR and structured extraction. Use `gemma4` for general images
where you want a conversational answer rather than structured document output.

## Key Capabilities

#### Basic Chat Completion

A text-only single-turn request. Only the `model` field differs from a Sarvam chat completion call.

#### Python

```python
from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

response = client.open_source_models.chat_completions_v2(
    model="gemma4",
    messages=[
        {"role": "user", "content": "Classify this support ticket as billing, technical, or other: 'My invoice shows twice the usage I expected.'"}
    ],
    temperature=0.1,
    max_tokens=100,
)

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

#### JavaScript

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

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

async function main() {
    const response = await client.openSourceModels.chatCompletionsV2({
        model: "gemma4",
        messages: [
            {
                role: "user",
                content: "Classify this support ticket as billing, technical, or other: 'My invoice shows twice the usage I expected.'",
            },
        ],
        temperature: 0.1,
        max_tokens: 100,
    });

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

main();
```

#### cURL

```bash
curl -X POST https://api.sarvam.ai/v2/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "Classify this support ticket as billing, technical, or other: \"My invoice shows twice the usage I expected.\""}
    ],
    "model": "gemma4",
    "temperature": 0.1,
    "max_tokens": 100
  }'
```

#### Image Input

Send an image as a content part alongside your text prompt. The image must be a
**base64 data URI** — see the note below.

#### Python

```python
import base64

from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

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

response = client.open_source_models.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}"},
                },
            ],
        }
    ],
    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.openSourceModels.chatCompletionsV2({
        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}` },
                    },
                ],
            },
        ],
        max_tokens: 500,
    });

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

main();
```

#### cURL

```bash
# Build the payload so the base64 image does not hit shell argument limits.
python3 - <<'PY'
import base64, json
encoded = base64.b64encode(open("chart.png", "rb").read()).decode()
json.dump({
    "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}"}},
        ],
    }],
    "max_tokens": 500,
}, open("payload.json", "w"))
PY

curl -X POST https://api.sarvam.ai/v2/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @payload.json
```

**Remote image URLs are rejected.** You must inline the image as a base64 data URI,
for example `data:image/png;base64,<encoded bytes>`. Passing an `http://` or
`https://` URL returns a validation error.

Because images are inlined, they count against the **10 MB** request cap. Base64
encoding adds roughly 33% overhead, so keep source images under about 7 MB.

#### Streaming

Set `stream` to `true` to receive tokens as server-sent events.

#### Python

```python
from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

stream = client.open_source_models.chat_completions_v2(
    model="gemma4",
    messages=[
        {"role": "user", "content": "Summarise what a load balancer does in three sentences."}
    ],
    max_tokens=300,
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
```

#### cURL

```bash
curl -N -X POST https://api.sarvam.ai/v2/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemma4",
    "messages": [
      {"role": "user", "content": "Summarise what a load balancer does in three sentences."}
    ],
    "max_tokens": 300,
    "stream": true
  }'
```

## Parameters

Gemma 4 31B accepts the standard OpenAI-compatible parameter set — `temperature`, `top_p`,
`max_tokens`, `stream`, `stop`, `n`, `seed`, `frequency_penalty`, `presence_penalty`,
`response_format` (Structured Outputs), `tools`, and `tool_choice`. See the
[full parameter table](/api/getting-started/models/open-source#supported-parameters) for
types, defaults, and ranges.

Model-specific notes:

* **Images must be base64 data URIs** — remote `http(s)` URLs are rejected.
* **No reasoning** — the model answers directly, never returns `reasoning_content`, and
  `reasoning_effort` is accepted but has no effect.
* **`seed` is reproducible** — identical seed and parameters return identical output in
  current serving.
* **Don't ask for JSON in the prompt when `response_format` is set** — an instruction
  like "Return JSON" alongside a JSON response format makes the model emit whitespace
  until it exhausts `max_tokens`, billing the full budget for no usable output. Let
  `response_format` handle the shape.

## Limits and errors

Common errors for every model on `/v2/chat/completions` are documented on the
[Open-Source Models overview](/api/getting-started/models/open-source#errors). Gemma-4-specific cases:

| Condition                                            | Status | Code                         | What it means                                                                                           |
| ---------------------------------------------------- | ------ | ---------------------------- | ------------------------------------------------------------------------------------------------------- |
| API key lacks beta access                            | `400`  | `invalid_request_error`      | Request whitelisting — see [Beta APIs](/api-reference/beta-apis).                                       |
| Missing or invalid API key                           | `403`  | `invalid_api_key_error`      | Check the `api-subscription-key` header.                                                                |
| No credits remaining                                 | `402`  | `insufficient_quota_error`   | Top up on the [dashboard](https://dashboard.sarvam.ai).                                                 |
| Unknown or unavailable model id                      | `404`  | `not_found_error`            | Confirm the id with [`GET /v2/models`](/api-reference/open-source/models).                              |
| `image_url` pointing at an `http(s)` URL             | `400`  | `invalid_request_error`      | Inline the image as a base64 data URI instead.                                                          |
| `max_tokens` above 131,072                           | `400`  | `invalid_request_error`      | `max_tokens` cannot exceed the context window.                                                          |
| Estimated prompt tokens + `max_tokens` above 131,072 | `422`  | `unprocessable_entity_error` | Shorten the prompt, send fewer images, or lower `max_tokens`.                                           |
| Request body above the model's size cap              | `413`  | `invalid_request_error`      | Encoded images count toward this cap.                                                                   |
| Invalid tool schema                                  | `400`  | `invalid_request_error`      | See [Tool schemas](/api/getting-started/models/open-source#tool-schemas).                               |
| Whitespace-only reply with `finish_reason: "length"` | `200`  | —                            | `response_format` was combined with a "return JSON" instruction in the prompt — remove the instruction. |

## Support

Sarvam supports the serving layer — auth, billing, rate limits, availability. Sarvam does
not tune or evaluate this model, so its output quality, reasoning, and language coverage
are properties of the model itself. See
[Open-Source Models](/api/getting-started/models/open-source) for the full support
boundary, and [Talk to us](/api/getting-started/help) for serving issues.