Skip to main content

Forwarding for Gemini Models

For the Gemini series, we provide two invocation methods: native API calls and OpenAI-compatible calls.
Before you start, make sure to install or update the native dependency by running either pip install google-genai or pip install -U google-genai.
1️⃣ For native integration, Gemini takes care of routing traffic between AI Studio and VertexAI automatically. Just supply your AIHubMix API key and the appropriate request URL. Remember, this URL is different from the usual base_url—follow the example below to ensure proper setup.
2️⃣ For OpenAI-compatible formats, retain the universal v1 endpoint.
3️⃣ For the 2.5 series, if you need to display the reasoning process, there are two ways to do it:
  1. Native invocation: Pass include_thoughts=True
  2. OpenAI-compatible method: Pass reasoning_effort
You can refer to the code examples below for detailed usage.

Gemini 3 Pro Image Preview Instructions

Gemini 3 Pro Image Preview (Nano Banana Pro Preview) is designed for professional asset creation and complex instructions. This model offers the following features:
  • Uses Google Search to retrieve real-time world knowledge
  • Built-in “thinking” process (optimizes composition before generation)
  • Can generate images with resolutions up to 4K
Streaming Mode (stream=True) → reasoning stage only
Non-Streaming Mode (stream=False) → final image generation
Generated images are not included in streaming responses and must be retrieved using a non-streaming request .
Python usage examples:

About Gemini 2.5 Inference Models

  1. The entire 2.5 series consists of inference models.
  2. 2.5 Flash is a hybrid model, similar to Claude Sonnet 3.7. You can fine-tune its reasoning behavior by adjusting the thinking_budget parameter for optimal control.
  3. 2.5 Pro is a pure inference model. Thinking cannot be disabled, and thinking_budget should not be explicitly set.
Python usage examples:

Gemini 2.5 Flash: Quick Task Support

Example for OpenAI-compatible invocation:
  1. For complex tasks, simply set the model id to the default gemini-2.5-flash-preview-04-17 to enable thinking.
  2. Gemini 2.5 Flash uses the budget parameter to control the depth of thinking, ranging from 0 to 16K. The default budget is 1024, and the optimal marginal effect is 16K.

Media Understanding

  • For multimedia files under 20MB (images, audio, video), upload them using inline_data.
  • When a multimedia file is larger than 20MB, you must use the Files API.

Files Under 20MB

By adding the EDIARESOLUTION_MEDIUM parameter, you can adjust the image resolution, which significantly reduces input costs and minimizes the risk of errors with large images.Supported media resolution values:
Python usage examples:

Files API

Gemini can handle various types of input data simultaneously, including text, images, and audio. When the total request size (including files, text hints, system commands, etc.) exceeds 20 MB, be sure to use the Files API.
  • Listing uploaded files is not supported.
  • Files will be automatically deleted after 48 hours, or you can manually delete uploaded files.
Python usage examples:

Code Execution

The code execution feature enables the model to generate and run Python code and learn iteratively from the results until it arrives at a final output. You can use this code execution capability to build applications that benefit from code-based reasoning and that produce text output. For example, you could use code execution in an application that solves equations or processes text.
Python

Interactions API

Interactions is Gemini’s next-generation inference interface that returns structured Interaction objects, supporting text generation, native image generation (Nano Banana), and multi-step reasoning. Synchronous mode (interactions.create()) is currently supported; asynchronous mode (Background Interactions) is coming soon.
SDK version requirement: @google/genai >= 2.0.0 (JS/TS) or google-genai >= 2.0.0 (Python). Older SDK versions calling Interactions will be rejected by Google’s backend (legacy Interactions schema no longer supported).

Text Generation

Call interactions.create() to initiate inference. The returned Interaction object provides an output_text convenience property.

Native Image Generation

Configure the output modality to image via response_format. The returned Interaction object provides an output_image convenience property.
  • Recommended model: gemini-3.1-flash-image (Nano Banana 2, general-purpose image generation model).
  • response_modalities values must be lowercase ['text', 'image']; uppercase is the generateContent API convention and will return 400 in the Interactions API.
  • Do not pass delivery: 'inline' (400 Image delivery mode is not supported) — results are returned inline by default.

Streaming

Pass stream: true to enable SSE streaming. Incremental text is obtained via event.delta.text.
JavaScript
For the full SDK integration guide (including Embeddings, explicit caching CRUD, capabilities matrix, and more), see Gemini Native SDK Integration.

Context caching

Gemini’s native API enables implicit context caching by default—no setup required. For every generate_content request, the system automatically caches the input content. If a subsequent request uses the exact same content, model, and parameters, the system will instantly return the previous result, dramatically speeding up response time and potentially reducing input token costs.
  • Caching is automatic—no manual configuration needed.
  • The cache is only hit when the content, model, and all parameters are exactly the same; any difference will result in a cache miss.
  • The cache time-to-live (TTL) can be set by the developer, or left unset (defaults to 1 hour). There is no minimum or maximum TTL enforced by Google. Costs depend on the number of cached tokens and the cache duration.
    • While Google places no restriction on TTL, as a forwarding platform, we only support a limited TTL range. For requirements beyond our platform’s limits, please contact us.

Notes

  • No guaranteed cost savings: Cache tokens are billed at 25% of the standard input price—so theoretically, caching can save you up to 75% of input token costs. However, Google’s official docs make no guarantee of cost savings; the real-world effect depends on your cache hit rate, token types, and storage duration.
  • Cache hit conditions: To maximize cache effectiveness, place repeatable context at the start of your input and dynamic content (like user input) at the end.
  • How to detect cache hits: If a response comes from the cache, response.usage_metadata will include the cache_tokens_details field and cached_content_token_count. You can use these to determine cache usage.
    Example fields when a cache hit occurs:
Code example:
When a cache hit occurs, response.usage_metadata will contain:
Core conclusion: Implicit caching is automatic and provides clear cache hit feedback. Developers can check usage_metadata for cache status. Cost savings are not guaranteed—actual benefit depends on request structure and cache hit rates.

Function calling

By using the openai compatible way to call Gemini’s function calling, you need to pass in tool_choice="auto" in the request body, otherwise it will report an error.
Output Example:

Token Usage Tracking Made Simple

  1. Gemini tracks token usage using usage_metadata. Here’s what each field means:
    • prompt_token_count: number of input tokens
    • candidates_token_count: number of output tokens
    • thoughts_token_count: tokens used during reasoning (also counted as output)
    • total_token_count: total tokens used (input + output)
    For more details, check out their official docs.
  2. For APIs using the OpenAI-compatible format, token usage is tracked under .usage with the following fields:
    • usage.completion_tokens: number of input tokens
    • usage.prompt_tokens: number of output tokens (including reasoning)
    • usage.total_tokens: total token usage

Here’s how to use it in code:

Last updated: 2026-07-07