# Gemini API Tutorial: generateContent, OpenAI Python SDK and Streaming

> Call Gemini through Passion8 using native generateContent with curl or the OpenAI-compatible Python SDK. Includes streaming and the correct API key, model and URL setup.

URL: https://docs.passion8.cc/en/docs/gemini/api
Language: en
Publisher: Passion8
Last updated: 2026-10-11

Connect Gemini through Passion8 using either the native Gemini protocol or the OpenAI compatible protocol. Both use a Passion8 key and an enabled console model; no Google account or Google API key is required.

## Choose a protocol

| Client | Address | Request format |
| --- | --- | --- |
| Gemini CLI / native Gemini | `https://passion8.cc` | `/v1beta/models/{model}:generateContent` |
| Cline OpenAI Compatible / OpenAI SDK | `https://passion8.cc/v1` | `/chat/completions` |

Google's own OpenAI compatible endpoint has its own path. Passion8's compatible endpoint is `/v1`; do not append Google's example path to it.

## Prepare environment variables

macOS / Linux:

```bash
export PASSION8_API_KEY="replace-with-your-Passion8-key"
export PASSION8_GEMINI_MODEL="replace-with-an-enabled-Gemini-model-ID"
```

Windows PowerShell:

```powershell
$env:PASSION8_API_KEY="replace-with-your-Passion8-key"
$env:PASSION8_GEMINI_MODEL="replace-with-an-enabled-Gemini-model-ID"
```

Copy the full model ID from the console. These variable names belong to this SDK example and do not update Gemini CLI. For CLI variables, see [installation and configuration](https://docs.passion8.cc/en/docs/gemini).

## Native Gemini: a minimal request

On macOS / Linux:

```bash
curl --fail-with-body \
  "https://passion8.cc/v1beta/models/${PASSION8_GEMINI_MODEL}:generateContent" \
  -H "x-goog-api-key: $PASSION8_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents":[{"role":"user","parts":[{"text":"Reply only OK."}]}]}'
```

Text appears in `candidates[].content.parts[].text`. If it is absent, inspect the error body or finish reason; an empty candidate is not evidence of a successful connection.

## OpenAI compatible: Python SDK

Install the SDK in your project's virtual environment:

```bash
python -m pip install --upgrade openai
```

Save this as `gemini_smoke.py`, then run `python gemini_smoke.py`:

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["PASSION8_API_KEY"],
    base_url="https://passion8.cc/v1",
)

response = client.chat.completions.create(
    model=os.environ["PASSION8_GEMINI_MODEL"],
    messages=[{"role": "user", "content": "Reply only OK."}],
)
print(response.choices[0].message.content)
```

This uses the OpenAI compatible protocol and does not load Gemini CLI configuration. Verify a regular request before testing streaming, images or tools; compatibility does not guarantee every native Gemini field.

### Stream output

Replace the request section with:

```python
stream = client.chat.completions.create(
    model=os.environ["PASSION8_GEMINI_MODEL"],
    messages=[{"role": "user", "content": "Explain this project in three sentences."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
```

Production code should handle authentication, rate limits and timeouts using the SDK's actual error types. Check the key for `401`; inspect error details and retry guidance for `429`. Do not repeatedly retry an invalid model ID.

## Verify results and capability boundaries

Check request time, model and billing in the Passion8 console. Images, structured outputs, function calls, search and caching are separate features to verify individually. Google's hosted Grounding or search documentation does not establish availability on every Passion8 channel.

For editor setup, see [Gemini in VS Code](https://docs.passion8.cc/en/docs/gemini/vscode). For project tasks, see [coding workflow](https://docs.passion8.cc/en/docs/gemini/workflow).

## API Setup Questions

### Can native Gemini and OpenAI-compatible requests use the same key?

Both protocols use a Passion8 key, without a Google API key. Access to the selected model still depends on token permissions. Native requests use `x-goog-api-key` and `https://passion8.cc/v1beta/models/{model}:generateContent`; the OpenAI SDK uses Bearer authentication and `https://passion8.cc/v1` as its base URL. Keep the URL and request body consistent with the protocol.

### Can I copy every Google example directly into the OpenAI SDK?

You cannot copy native `contents` and `parts` request bodies directly into Chat Completions. OpenAI-compatible requests use `messages`; native requests use `contents`. Verify images, tools, search and caching separately for the protocol and Passion8 route you choose.

## Verified sources

Checked: 2026-10-11.

- [Gemini OpenAI compatibility](https://ai.google.dev/gemini-api/docs/openai)
- [Gemini generateContent API](https://ai.google.dev/api/generate-content)
- [Gemini CLI configuration](https://geminicli.com/docs/reference/configuration/)
