Skip to main content

Google generateContent

The gateway serves the Google AI Studio and Vertex AI generateContent format at /v1beta/models/{model}:generateContent and :streamGenerateContent. Clients built on the Google Gen AI SDK keep their request format while the gateway routes to the model behind the public name, which can be a Gemini model or a model from any other provider. For Gemini models on AI Studio and Vertex AI, requests are forwarded natively.

Scenario​

A mobile team built its app on the Google Gen AI SDK. The company now routes every model call through the gateway for cost tracking and wants the option to serve some requests from a Claude model. The team changes the SDK's base URL to the gateway and uses a virtual key in place of the Google API key. Nothing else in the app changes, and the admin can swap the model behind the public name at any time.

Steps​

  1. Register the model. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints), click Add Model, pick Google AI Studio or Vertex AI as the Provider, choose the model, set the Public Model Name to gemini-3.8-flash and click Add Model.

  2. Create a key in Virtual Keys (/ui/api-keys) with + Create New Key and pick the model under Models.

  3. Send a request. The virtual key goes in the x-goog-api-key header, in the key query parameter, or as a bearer token.

    curl -X POST "http://localhost:4000/v1beta/models/gemini-3.8-flash:generateContent" \
    -H "x-goog-api-key: $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "contents": [{"role": "user", "parts": [{"text": "Explain a model group in one sentence."}]}],
    "generationConfig": {"temperature": 0.2, "maxOutputTokens": 200}
    }'
  4. Point the SDK at the gateway. With the Google Gen AI SDK set http_options={"base_url": "http://localhost:4000", "api_version": "v1beta"} and api_key to the virtual key. The version matters, because SDK 3 defaults to v1, which the gateway does not serve for generateContent and answers with 405. The Python and JavaScript SDK pages cover streaming, errors and the environment variables.

  5. Check the log. Open Logs (/ui/logs) and find the request with its tokens and cost.

Expected result​

{
"candidates": [
{"content": {"role": "model", "parts": [{"text": "A model group is a public name for deployments."}]}, "finishReason": "STOP"}
],
"usageMetadata": {"promptTokenCount": 12, "candidatesTokenCount": 28, "totalTokenCount": 40}
}

:streamGenerateContent streams partial candidates as server-sent events.

Request fields​

The model in the path is a public model name from the gateway's model_list. contents, systemInstruction, generationConfig, safetySettings, tools, toolConfig and cachedContent are supported, and generationConfig.thinkingConfig controls reasoning on models that support it.

Other providers behind the Gemini format​

import os
from google import genai

client = genai.Client(
api_key=os.environ["SPANWAY_API_KEY"],
http_options={"base_url": "http://localhost:4000", "api_version": "v1beta"},
)
response = client.models.generate_content(model="claude-sonnet-5-5", contents="Hello from a Gemini client")
print(response.text)

Here an Anthropic model answers a Gemini format request. Function declarations, function responses, inline data and file data parts are translated between formats.

POST /v1beta/models/{model}:countTokens counts tokens for a request and answers with totalTokens. The Interactions API serves Google's agent oriented format, and Gemini managed agents create named agents on the Gemini side. Native Gemini endpoints beyond generateContent are available through the Google AI Studio pass-through and the Vertex AI pass-through.

Gateway features​

Keys, budgets, routing, guardrails, caching and cost tracking apply to these routes exactly as to chat completions.

Errors​

Errors the gateway raises come back in the OpenAI shape, {"error": {"message": "...", "type": "...", "param": null, "code": "400"}}, with no Google status field. The Google Gen AI SDK still raises by HTTP status, ClientError for a 4xx answer and ServerError for a 5xx answer in Python, and ApiError with a status in JavaScript.

StatusWhat it meansFix
400contents is missing or a part is malformed, or the model name in the path is not a public model on the gateway (Invalid model name passed in model=<name>).Send at least one content with a parts list, and call GET /v1/models to see the names the key may use.
401No key in x-goog-api-key, key or Authorization, or the key is wrong or expired.Send a live virtual key.
403The key may not call this model (The requested model '<model>' is not available for this API key, or the model name is invalid.).Add the model to the key. A guardrail block is a 400, read its result in Logs.
422The key, team or user has spent its budget (Budget has been exceeded!).Raise the budget or wait for it to reset.
429The key is over its rate limit, or the provider is throttling.Raise the limit or add a fallback.

See the client error reference for every status and the SDK exception it maps to.

Logs​

Each call is one row on the Logs page (/ui/logs) with its key, team, model group, deployment, tokens, cost, latency and status, failed calls included. Prompt and response text is kept only when the gateway is set to store it, see what the gateway stores. Operator log lines for routing, retries and fallbacks are in the log reference.

Providers​

The gateway's endpoint support matrix marks 2 provider integrations as supporting this endpoint:

Google AI Studio - Gemini, Google - Vertex AI.