Skip to main content
Google’s officially recommended SDK is google-genai (the new one, replacing the older google-generativeai). Set http_options.base_url to point requests at OpenPAI.

Python

pip install google-genai
from google import genai

client = genai.Client(
    api_key="sk-XXXXXXXXXXXXXXXX",
    http_options={"base_url": "https://openp.ai"},
)

resp = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="Introduce OpenPAI in one sentence",
)
print(resp.text)

JavaScript / TypeScript

npm install @google/genai
import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({
  apiKey: process.env.OPENPAI_API_KEY!,
  httpOptions: { baseUrl: "https://openp.ai" },
});

const resp = await client.models.generateContent({
  model: "gemini-3.1-pro-preview",
  contents: "hello",
});
console.log(resp.text);

Streaming

stream = client.models.generate_content_stream(
    model="gemini-3.1-pro-preview",
    contents="Tell a 100-word story",
)
for chunk in stream:
    print(chunk.text, end="", flush=True)

Multimodal

from google.genai import types

resp = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents=[
        types.Part.from_uri(file_uri="https://example.com/x.png", mime_type="image/png"),
        "Describe this image.",
    ],
)

Function calling

weather_tool = types.Tool(function_declarations=[
    types.FunctionDeclaration(
        name="get_weather",
        description="Get weather of a city",
        parameters={
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    )
])

resp = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="Weather in Beijing",
    config=types.GenerateContentConfig(tools=[weather_tool]),
)

Thinking mode

resp = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="Prove Goldbach's conjecture",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_budget=8000),
    ),
)
Or just use a suffixed model ID (simpler):
resp = client.models.generate_content(
    model="gemini-2.5-pro-thinking",
    contents="...",
)

Embeddings

resp = client.models.embed_content(
    model="text-embedding-004",
    contents=["hello world"],
)
print(resp.embeddings[0].values[:5])

Notes

  • When using the Gemini native protocol, the base URL is without /v1 (the SDK appends the /v1beta/models/... path automatically).
  • If your code originally uses the OpenAI-compatible path (/v1/chat/completions), you can leave it unchanged and just switch the model ID to gemini-*.
  • The OpenAI-compatible protocol automatically maps Gemini’s parts / inline_data to the content array, and vice versa.