> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# google-genai SDK

> Call Gemini through OpenPAI with Google's official SDK

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

```bash theme={null}
pip install google-genai
```

```python theme={null}
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

```bash theme={null}
npm install @google/genai
```

```ts theme={null}
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

```python theme={null}
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

```python theme={null}
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

```python theme={null}
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

```python theme={null}
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):

```python theme={null}
resp = client.models.generate_content(
    model="gemini-2.5-pro-thinking",
    contents="...",
)
```

## Embeddings

```python theme={null}
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.
