Skip to main content
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[].text
  • type:"image_url" / type:"input_audio" / type:"file" → Downloaded/decoded into parts[].inlineData with MIME allowlist:
    • 图片:image/pngimage/jpegimage/jpgimage/webpimage/heicimage/heif
    • 音频:audio/mpegaudio/mp3audio/wav
    • 视频:video/mp4video/movvideo/mpegvideo/mpgvideo/avivideo/wmvvideo/mpegpsvideo/flv
    • 文档:application/pdftext/plain
  • content 字符串里夹的 ![alt](data:image/...;base64,...) Markdown images become inlineData parts (same as image_url).

4. Tools passthrough

Three special names map to native Gemini tools; other 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 field max_tokens 设置的值,也可以用 extra_body.google.safetySettings 完全替换平台默认安全设置,新增 Gemini field(如未来上线的Field)无需改代码即可直接使用。

5.4 Passthrough example


6. Response format

6.1 Non-streaming chat.completion

  • id = Upstream responseId (matches log request_id); falls back to chatcmpl-* if missing.
  • reasoning_content:Thinking text (only when include_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_reason Mapping: STOP→stop, MAX_TOKENS→length, safety/recitation/…→content_filter, functionCalltool_calls.

6.2 Streaming chat.completion.chunk

  • In streaming, delta.content is a string (not an array).
  • Images embedded in delta.content as ![image](data:...) markdown.
  • id Stable across streaming chunks.

7. Usage

response.usage 完整Field:

7.1 How thinking tokens are counted

  • reasoning_tokens Shown separately for visibility.
  • completion_tokens includes reasoning_tokens (OpenAI semantics; billing uses completion_tokens).

7.2 Output token breakdown

系统会根据Output自动Bucket token Type:
Bucket基于Output,而非模型名——即使模型名含 “image”,纯文本回答仍按文本计费。

7.3 Modality case handling

Accepts image / IMAGE case variants.

8. Logging and reconciliation

Upstream token usage is logged per request:
  • responseId Matches response.id and log request_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:
Mapping (automatic):

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)

Gemini 3 series用 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:
In streaming, thinking arrives via delta.reasoning_content.

Q3:Gemini 2.5 vs 3 thinking differences


12. Known limitations


13. Troubleshooting