---
title: "Google GenerateContent"
subtitle: "Call models with the Google GenAI SDK, or call Gemini models with the OpenAI format."
---

The gateway serves Gemini's `generateContent` API. Point the Google GenAI SDK at it and pass your Respan API key as the SDK's key. If your code uses the OpenAI SDK, call Gemini models through [Chat Completions](#openai-format) instead.

`POST https://gateway.respan.ai/v1beta/models/{model}:generateContent`

<Tabs>
<Tab title="cURL">
```bash
curl "https://gateway.respan.ai/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"role": "user", "parts": [{"text": "Hello!"}]}]}'
```
</Tab>
<Tab title="Python">
```python
import os
from google import genai

client = genai.Client(
    api_key=os.environ["RESPAN_API_KEY"],
    http_options={"base_url": "https://gateway.respan.ai"},
)

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Hello!",
)
print(response.text)
```
</Tab>
<Tab title="TypeScript">
```typescript
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: process.env.RESPAN_API_KEY,
  httpOptions: { baseUrl: "https://gateway.respan.ai" },
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "Hello!",
});
console.log(response.text);
```
</Tab>
</Tabs>

The gateway reads your Respan key from `x-goog-api-key`. For a stream, see [Streaming](#streaming).

## Ways to reach Gemini

| Way | Path | Routed, billed, and translated | Key for Google |
|---|---|---|---|
| [Gemini API format](#gemini-api-format) | `POST /v1beta/models/{model}:generateContent` | Yes | Respan credits or your own |
| [OpenAI format](#openai-format) | `POST /v1/chat/completions` | Yes | Respan credits or your own |
| [Gemini passthrough](#gemini-passthrough) | `POST /api/google/gemini/v1beta/models/{model}:{action}` | No, only logged | Your own only |

## Gemini API format

These requests are routed like any other, so routes, BYOK, limits, and logging apply, and a model from another provider can answer in Gemini's shape. To pin a provider, send the `X-Respan-Route-Provider` header.

Gemini-only fields with no equivalent elsewhere, such as `safetySettings`, `cachedContent`, and Google-run tools like `googleSearch`, reach Gemini models unchanged and are refused with `400` for other providers' models. See [known gaps](/docs/oss-gateway/api-formats#chat-gaps).

Send Respan params in the `X-Data-Respan-Params` header, in a top-level `respan_params` object in the body, or in `metadata.respan_params`.

## Streaming

Call `:streamGenerateContent?alt=sse` (the SDK's `generate_content_stream`) and the gateway returns server-sent events in Gemini's shape, whichever provider answers. Each event is a `data: {GenerateContentResponse}` with partial `candidates`, and the stream ends when the connection closes. Usage is in `usageMetadata` on the last chunk.

```bash
curl -N "https://gateway.respan.ai/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"role": "user", "parts": [{"text": "Write a haiku about routers."}]}]}'
```

For errors before and during a stream, cancellation and billing, and the gateway's fixed 600 second timeout, see [Chat Completions](/docs/ai-gateway/requests/chat-completions#stream-errors).

## Structured outputs

Gemini's structured output setting is `responseMimeType: application/json` with a `responseJsonSchema` in `generationConfig`. It is also what the gateway sends to Gemini models when a Chat Completions request carries a `json_schema` `response_format`; a `json_object` format becomes `responseMimeType: application/json` alone. Gemini always enforces the schema strictly.

```bash
curl "https://gateway.respan.ai/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "Ada Lovelace was born in London in 1815."}]}],
    "generationConfig": {
      "responseMimeType": "application/json",
      "responseJsonSchema": {
        "type": "object",
        "properties": {
          "name": {"type": "string"},
          "birth_year": {"type": "integer"},
          "city": {"type": "string"}
        },
        "required": ["name", "birth_year", "city"]
      }
    }
  }'
```

From Chat Completions, a `json_schema` with a `description` or `strict: false` is refused with `400` for Gemini models. See [Model support](/docs/ai-gateway/requests/chat-completions#model-support). The gateway doesn't repair JSON; see [Invalid JSON](/docs/ai-gateway/requests/chat-completions#invalid-json).

{/* REVIEW: new section. Source pages only document responseJsonSchema as the translation target from Chat Completions; confirm it (and Gemini's older responseSchema) passes through unchanged on the Gemini API format, and how it is handled when the request goes to another provider's model. */}

## Tool calling

Tools use Gemini's shape: function definitions go in `tools[].functionDeclarations`, each with a `name`, a `description` and `parameters`. When the model calls one, the candidate's content has a `functionCall` part with the `name` and `args`. Send the result back in the next request as a `functionResponse` part.

```bash
curl "https://gateway.respan.ai/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "What is the weather in Paris?"}]}],
    "tools": [{
      "functionDeclarations": [{
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }]
    }]
  }'
```

The answer carries the call instead of text:

```json
{"candidates": [{"content": {"role": "model", "parts": [
  {"functionCall": {"name": "get_weather", "args": {"city": "Paris"}}}
]}}]}
```

Google-run tools such as `googleSearch` reach Gemini models only; see [Gemini API format](#gemini-api-format). For the agent loop, `tool_choice`, routing by tool support, and how tool calls are logged and billed, see [Tool calling](/docs/ai-gateway/requests/chat-completions#tool-calling) on Chat Completions.

{/* REVIEW: new section. Source pages describe tool translation and server tools for Chat Completions only; confirm functionDeclarations / functionCall / functionResponse are translated for other providers' models on the Gemini API format and whether server tools run on this format. */}

## OpenAI format

Call a Gemini model from the OpenAI SDK by its catalog ID:

```python
response = client.chat.completions.create(
    model="gemini/gemini-2.5-flash",
    messages=[{"role": "user", "content": "Hello!"}],
)
```

The gateway calls Gemini on its native API, not Gemini's OpenAI-compatible endpoint, so PDF and other file parts in messages work. OpenAI parameters are renamed for Gemini: `max_tokens` becomes `max_output_tokens`, `n` becomes `candidate_count`, `stop` becomes `stop_sequences`. See [Parameters](/docs/ai-gateway/requests/chat-completions#request-body) and [Chat Completions](/docs/ai-gateway/requests/chat-completions).

## Gemini passthrough

The passthrough sends your request body to Google's Gemini API exactly as written. Use it when you need a Gemini feature the routed format can't carry.

```python
client = genai.Client(
    api_key=os.environ["RESPAN_API_KEY"],
    http_options={"base_url": "https://gateway.respan.ai/api/google/gemini"},
)
```

With a Gemini key of your own (saved with [BYOK](/docs/ai-gateway/spend/credits-vs-byok#byok) or sent with the request), the call runs on it and the gateway only logs it, without routing, translating, or billing it. Without one, it runs on Respan credits on Gemini. See [Passthrough](/docs/ai-gateway/requests/other-endpoints#provider-passthrough).

## Next steps

- [Gemini CLI](/docs/integrations/gateway/gemini-cli) and [Google GenAI SDK](/docs/integrations/gateway/google-gen-ai) guides.
- [Provider routing](/docs/ai-gateway/routing/provider-routing).
