跳转到主要内容

Gemini OpenAI 兼容格式

你可以通过 OpenAI Chat Completions 兼容格式调用 Gemini。
POST /v1/chat/completions
本文页只写入 2026-03-22 已在 Crazyrouter 生产环境复核过的兼容层行为。 当前主示例模型:
  • gemini-3-pro-preview

当前主结论

本轮生产复核确认:
  • gemini-3-pro-preview 可以通过 /v1/chat/completions 正常对话
  • 流式返回兼容 chat.completion.chunk SSE 结构
  • 如果你需要 Gemini 的结构化输出、Google Search、thinking 等能力,当前仍应优先看 Gemini 原生格式

基本对话

cURL
curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gemini-3-pro-preview",
    "messages": [
      {
        "role": "system",
        "content": "You are a concise assistant."
      },
      {
        "role": "user",
        "content": "Explain quantum entanglement in one sentence."
      }
    ],
    "max_tokens": 128
  }'
当前生产验证返回的关键结构:
{
  "object": "chat.completion",
  "model": "gemini-3-pro-preview",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "..."
      }
    }
  ]
}

Python 示例

Python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://crazyrouter.com/v1"
)

response = client.chat.completions.create(
    model="gemini-3-pro-preview",
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "Explain quantum entanglement in one sentence."}
    ],
    max_tokens=128
)

print(response.choices[0].message.content)

流式输出

本轮生产也复核了流式兼容返回:
Python
stream = client.chat.completions.create(
    model="gemini-3-pro-preview",
    messages=[
        {"role": "user", "content": "Write one short sentence about AI."}
    ],
    max_tokens=64,
    stream=True
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content is not None:
        print(delta.content, end="")
当前生产收到的 SSE 片段形态类似:
data: {"object":"chat.completion.chunk","model":"gemini-3-pro-preview",...}
data: {"choices":[{"delta":{"content":"..."}}],...}
data: [DONE]

何时用兼容格式,何时用原生格式

  • 已有 OpenAI SDK 代码,且只需要标准对话:优先用兼容格式
  • 需要 Gemini 的结构化输出、Google Search、thinking、原生字段:优先用 Gemini 原生格式
这页仍然只覆盖 /v1/chat/completions 兼容层,不把 Gemini 背后的图片能力混进来。如果你要接 Crazyrouter 当前已经收敛的公开图片协议,请直接看 Nano Banana family 的 /v1/images/generations 页面: Nano BananaNano Banana ProNano Banana 2