# Gemma 4 31B

> Google's open-weight model, released April 2026, accepting text, image and video input. It is very cheap and efficient but ranks low on most benchmarks.

Use it with Pydantic AI as `gateway/doubleword:google/gemma-4-31B-it`.

## Spec sheet

- **Family:** Gemini
- **Released:** 2 Apr 2026
- **Weights:** open
- **Context window:** 262,144 tokens
- **Largest response:** 32,768 tokens
- **Accepts:** text, image, video
- **Tool calling:** yes
- **Knowledge cutoff:** unknown
- **Intelligence index:** 51.2 (rank 83)
- **Price:** $0.12 input, $0.35 output, $0.18 blended per 1M tokens
- **Index points per $:** 288.5 (rank 7)
- **Speed:** 32.5 tokens/sec (rank 164)
- **Popularity:** rank 35 of 194 by usage
- **Pydantic AI Gateway:** built in

## Benchmarks

| Benchmark | Score | Rank among models tested | What it measures | Source |
| --- | --- | --- | --- | --- |
| Intelligence index | 51.2 | 86 | Our composite of the benchmarks below, 0-100 | Pydantic, from the sources credited below |
| GPQA Diamond | 75.8% | 85 | Graduate-level science questions, multiple choice | Epoch AI (CC BY 4.0) |
| OTIS Mock AIME | 73.3% | 84 | Competition mathematics problems with exact answers | Epoch AI (CC BY 4.0) |
| SimpleQA Verified | 10.4% | 76 | Short factual questions answered from memory, without search | Epoch AI (CC BY 4.0) |
| SciCode | 45.5% | 67 | Scientific programming problems written by researchers | SciCode, via Epoch AI (CC BY 4.0) |
| WebDev Arena | 1364.1 | 73 | Rating from human votes on web apps the models built | LMArena, via Epoch AI (CC BY 4.0) |
| LMArena text | 1442.9 | 50 | Rating from blind human votes between pairs of chat responses | LMArena (CC BY 4.0) |
| Epoch Capabilities Index | 144.1 | 86 | Epoch AI's own composite across about 40 benchmarks; shown for comparison, not part of our index | Epoch AI (CC BY 4.0) |

## Providers and prices

US dollars per million tokens.

| Provider | Gateway | Pydantic AI string | Input | Cached input | Output |
| --- | --- | --- | --- | --- | --- |
| Doubleword | built in | `gateway/doubleword:google/gemma-4-31B-it` | $0.12 | – | $0.35 |
| Novita | BYOK | – | $0.14 | – | $0.4 |
| OpenRouter | BYOK | `openrouter:google/gemma-4-31b-it` | $0.15 | $0.06 | $0.4 |

## Example code

### hello.py

Ask Gemma 4 31B a question with a Pydantic AI agent and print the answer. Run it with uv run hello.py.

```python
# /// script
# dependencies = ["pydantic-ai"]
# ///
import os

from pydantic_ai import Agent

os.environ['PYDANTIC_AI_GATEWAY_API_KEY'] = '...'

agent = Agent('gateway/doubleword:google/gemma-4-31B-it', instructions='Be concise, reply with one sentence.')

result = agent.run_sync('Where does "hello world" come from?')
print(result.output)
```

### structured_output.py

Get validated, typed output from Gemma 4 31B by giving the agent a Pydantic model. Run it with uv run structured_output.py.

```python
# /// script
# dependencies = ["pydantic-ai"]
# ///
import os

from pydantic import BaseModel
from pydantic_ai import Agent

os.environ['PYDANTIC_AI_GATEWAY_API_KEY'] = '...'


class CityLocation(BaseModel):
    city: str
    country: str


agent = Agent('gateway/doubleword:google/gemma-4-31B-it', output_type=CityLocation)

result = agent.run_sync('Where were the olympics held in 2012?')
print(result.output)
#> city='London' country='United Kingdom'
```

### tools.py

Give Gemma 4 31B two tools to call, one of which reads the agent's dependencies. Run it with uv run tools.py.

```python
# /// script
# dependencies = ["pydantic-ai"]
# ///
import os
import random

from pydantic_ai import Agent, RunContext

os.environ['PYDANTIC_AI_GATEWAY_API_KEY'] = '...'

agent = Agent(
    'gateway/doubleword:google/gemma-4-31B-it',
    deps_type=str,
    instructions="You're a dice game, roll the die and see if it matches the user's guess.",
)


@agent.tool_plain
def roll_dice() -> str:
    """Roll a six-sided die and return the result."""
    return str(random.randint(1, 6))


@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
    """Get the player's name."""
    return ctx.deps


result = agent.run_sync('My guess is 4', deps='Anne')
print(result.output)
```

### streaming.py

Stream Gemma 4 31B's reply to the terminal as it is generated. Run it with uv run streaming.py.

```python
# /// script
# dependencies = ["pydantic-ai"]
# ///
import asyncio
import os

from pydantic_ai import Agent

os.environ['PYDANTIC_AI_GATEWAY_API_KEY'] = '...'

agent = Agent('gateway/doubleword:google/gemma-4-31B-it')


async def main():
    async with agent.run_stream('Explain prompt caching in three bullet points.') as response:
        async for text in response.stream_text(delta=True):
            print(text, end='', flush=True)


asyncio.run(main())
```

### pax.sh

Chat with Gemma 4 31B in your terminal using pax, the TUI for pydantic-ai-harness. Coming soon.

```sh
export PYDANTIC_AI_GATEWAY_API_KEY='...'

uvx pax --model 'gateway/doubleword:google/gemma-4-31B-it'
```

### codex.sh

Run OpenAI's Codex CLI on Gemma 4 31B through Pydantic AI Gateway.

```sh
# npm install -g @openai/codex
export PYDANTIC_AI_GATEWAY_API_KEY='...'

codex --model 'google/gemma-4-31B-it' \
  -c model_provider='gateway' \
  -c model_providers.gateway.name='Pydantic AI Gateway' \
  -c model_providers.gateway.base_url='https://gateway.pydantic.dev/proxy/doubleword/v1' \
  -c model_providers.gateway.env_key='PYDANTIC_AI_GATEWAY_API_KEY'
```

### ai_sdk.ts

Call Gemma 4 31B from TypeScript with the Vercel AI SDK, through Pydantic AI Gateway.

```ts
// npm install ai @ai-sdk/openai-compatible
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'
import { generateText } from 'ai'

process.env.PYDANTIC_AI_GATEWAY_API_KEY = '...'

const doubleword = createOpenAICompatible({
  baseURL: 'https://gateway.pydantic.dev/proxy/doubleword',
  apiKey: process.env.PYDANTIC_AI_GATEWAY_API_KEY,
})

const { text } = await generateText({
  model: doubleword('google/gemma-4-31B-it'),
  system: 'Be concise, reply with one sentence.',
  prompt: 'Where does "hello world" come from?',
})
console.log(text)
```

## About Gemma 4 31B

Gemma 4 31B is an open-weight model from Google, released April 2026, that accepts text, image and video and offers a 262k context window with up to 32k output tokens. It supports tool calling, structured output and a reasoning mode. Its main appeal is cost: input is $0.12 and output $0.35 per million tokens, a blended $0.18 against a registry median of about $1.01, which gives it seventh place for intelligence per dollar.

On capability it sits in the lower part of the registry. Its intelligence index is 51.2 against a median of 59.5, ranking 86th of 114. It scores 75.8% on GPQA Diamond and 73.3% on OTIS Mock AIME, both well below median, and 10.4% on SimpleQA Verified, last of 76 models tested. Its LMArena text rating of 1442.9 is slightly above the median, so chat preference is roughly typical.

**Strengths**

- Seventh of 194 for intelligence per dollar, at 288.5 points
- Blended price $0.18 per million tokens, 24th cheapest of 194
- 262k context window with image and video input
- LMArena text rating 1443, 50th of 114, slightly above median

**Weaknesses**

- Intelligence index 51.2, 86th of 114, below median 59.5
- SimpleQA Verified 10.4%, last of 76 models tested
- GPQA Diamond 75.8%, 85th of 94; OTIS Mock AIME 84th of 95

**Best for:** Low-cost, high-volume agent tasks needing tool calling, long context or image input, where top benchmark scores matter less.

Prices come from [genai-prices](https://github.com/pydantic/genai-prices), in US dollars per million tokens. Benchmark scores come from Epoch AI and LMArena (CC BY 4.0); the index and its sources are described at https://models.pydantic.dev/methodology.md. Data built 2026-11-25. Compare with other models: https://models.pydantic.dev/models.md
