Gemini OpenAI format (Chat)
Chat & text
Gemini OpenAI format (Chat)
Call Gemini chat and multimodal models via OpenAI-compatible /v1/chat/completions
POST
Gemini OpenAI format (Chat)
For Google Gemini native protocol, see Gemini Native. For general multi-model Chat Completions, see OpenAI Chat Completions.
Endpoint: POST https://api.gravitex.ai/v1/chat/completions
1. Model categories
2. Endpoint and authentication
3. OpenAI field → Gemini mapping
messages.content 支持多模态数组(OpenAI v2):
type:"text"→parts[].texttype:"image_url"/type:"input_audio"/type:"file"→ Downloaded/decoded intoparts[].inlineDatawith MIME allowlist:- 图片:
image/png、image/jpeg、image/jpg、image/webp、image/heic、image/heif - 音频:
audio/mpeg、audio/mp3、audio/wav - 视频:
video/mp4、video/mov、video/mpeg、video/mpg、video/avi、video/wmv、video/mpegps、video/flv - 文档:
application/pdf、text/plain
- 图片:
content字符串里夹的Markdown images becomeinlineDataparts (same asimage_url).
4. Tools passthrough
function entries use functionDeclarations.
5. extra_body — passthrough Gemini native parameters
extra_body.google.* All fields under this namespace are passed to the Gemini native API.
5.1 Two passthrough paths
5.2 thinking_config (snake_case allowlist)
只要传了 extra_body.google,系统自动的思维链适配会关闭,全部 thinking Behavior由调用方掌控。
5.3 Deep-merge rules
- Treat
extra_body.google(minus the one snake_case key above) as patch. - 把已经根据 OpenAI field构造好的 Gemini 请求作为 base。
- deep merge:
- Same key, both maps → recursive merge;
- 其它Type(标量、数组、null)→ patch 直接覆盖 base;
- Keys only in base are kept.
- Merged body is sent upstream — equivalent to a native Gemini call.
含义:你可以用extra_body.google.generationConfig.maxOutputTokens覆盖通过 OpenAI fieldmax_tokens设置的值,也可以用extra_body.google.safetySettings完全替换平台默认安全设置,新增 Gemini field(如未来上线的Field)无需改代码即可直接使用。
5.4 Passthrough example
6. Response format
6.1 Non-streaming chat.completion
id= UpstreamresponseId(matches logrequest_id); falls back tochatcmpl-*if missing.reasoning_content:Thinking text (only wheninclude_thoughts:true).executable_code/code_execution_result:Embedded as markdown code blocks in text.- Non-image media (audio, etc.) embedded as markdown
[media](data:...). finish_reasonMapping:STOP→stop,MAX_TOKENS→length, safety/recitation/…→content_filter,functionCall→tool_calls.
6.2 Streaming chat.completion.chunk
- In streaming,
delta.contentis a string (not an array). - Images embedded in
delta.contentasmarkdown. idStable across streaming chunks.
7. Usage
response.usage 完整Field:
7.1 How thinking tokens are counted
reasoning_tokensShown separately for visibility.completion_tokensincludesreasoning_tokens(OpenAI semantics; billing usescompletion_tokens).
7.2 Output token breakdown
系统会根据Output自动Bucket token Type:Bucket基于Output,而非模型名——即使模型名含 “image”,纯文本回答仍按文本计费。
7.3 Modality case handling
Acceptsimage / IMAGE case variants.
8. Logging and reconciliation
Upstream token usage is logged per request:responseIdMatchesresponse.idand logrequest_id.
9. Examples
9.1 Text chat with thinking
9.2 Multimodal input (text + image URL)
9.3 Google Search + URL context
9.4 Streaming chat
10. OpenAI-only parameters (ignored for Gemini)
以下 OpenAI 标准参数在 Gemini API 中没有对应Field,传入后会被静默丢弃,不会报错:11. FAQ — Thinking / Reasoning
Q1:Can I control Gemini thinking length in OpenAI format?
Yes. Three approaches:方式一:reasoning_effort(OpenAI 标准Field,最简单)
Pass OpenAI reasoning_effort; mapped to Gemini thinking config:
Option 2: model name suffix
Example:
gemini-3.5-flash-thinking-16384 → thinking on, budget 16384.
Option 3: extra_body.google.thinking_config (full control)
thinking_level 代替 thinking_budget:
优先级:extra_body.google.thinking_config>reasoning_effort> Suffix。传了extra_body.google后系统自动的思维链适配会关闭,所有 thinking Behavior完全由调用方控制。
Q2:How do I read thinking output?
设了include_thoughts: true 后,思考过程会放在响应的 reasoning_content Field:
delta.reasoning_content.
