Use Gemini with the OpenAI SDK Through Compatible Endpoints
Call Gemini from the OpenAI Python or TypeScript SDK via Google's OpenAI-compatible base URL or a multi-provider gateway, with current model aliases.
You can call Gemini from the official OpenAI SDK by setting base_url to Google's OpenAI-compatible endpoint (or to an AI gateway that fronts Gemini), authenticating with a Gemini API key or gateway key, and selecting a Gemini model ID in a normal chat.completions request. Google documents this path in the Gemini OpenAI compatibility guide. Teams that already standardized on the OpenAI client shape can evaluate Gemini without rewriting every integration.
Google also notes that if you are not already on the OpenAI libraries, calling the Gemini API directly is the recommended default for full feature access. Compatibility is a migration and evaluation bridge, not a claim that every OpenAI surface exists on Gemini.
Why OpenAI compatibility speeds Gemini trials
Many codebases already depend on the openai package, LangChain OpenAI chat models, or hand-written clients for /v1/chat/completions. Forcing every experiment onto the native Gemini SDK multiplies auth helpers, streaming parsers, and error handling. Compatibility lets you:
- keep one request builder and response parser
- A/B test Gemini against existing OpenAI-shaped traffic
- move credentials behind a gateway while clients stay simple
- reuse the same agent tool schemas during early evaluation
Compatibility is still a translation layer. Google describes OpenAI library support as expanding while in beta for some surfaces. Read the current limitations section before promising feature parity in a design review.
Minimal client setup
Direct Google OpenAI-compatible endpoint
Google's documented Python pattern uses the Generative Language OpenAI-compatible base path. Official samples currently use gemini-3.5-flash:
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["GEMINI_API_KEY"],
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)
response = client.chat.completions.create(
model="gemini-3.5-flash",
messages=[
{"role": "system", "content": "Answer with concrete engineering guidance."},
{
"role": "user",
"content": "When should a team try Gemini through an OpenAI client?",
},
],
)
print(response.choices[0].message.content)
TypeScript:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.GEMINI_API_KEY,
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
const response = await client.chat.completions.create({
model: "gemini-3.5-flash",
messages: [
{ role: "system", content: "Answer with concrete engineering guidance." },
{
role: "user",
content: "When should a team try Gemini through an OpenAI client?",
},
],
});
console.log(response.choices[0]?.message?.content);
Model IDs move. Prefer Google's models list over hardcoding older Flash or Pro snapshot strings in production configs.
Gateway path for multi-provider products
When Gemini is one channel among many, point the same SDK at your gateway:
client = OpenAI(
api_key="GATEWAY_API_KEY",
base_url="https://your-gateway.example.com/v1",
)
response = client.chat.completions.create(
model="gemini-flash", # stable alias mapped to gemini-3.5-flash (or successor)
messages=[{"role": "user", "content": "Draft a checklist for model A/B tests."}],
)
Register the upstream Gemini credential once as a channel, publish the alias in your model catalog, and keep application secrets limited to gateway keys. That is the same operational model described for OpenAI-compatible API gateways.
Stable aliases versus raw upstream IDs
Application configs should depend on operator-controlled aliases:
- Publish names such as
gemini-flashandgemini-pro(or product-specific labels). - Map those aliases to current upstream IDs (
gemini-3.5-flash, a Pro-class ID, and so on). - Change only the mapping when Google ships a replacement model.
- Run a short regression suite after each mapping change.
This keeps client PRs rare and makes rollback a configuration edit. Pair alias discipline with latency versus cost model routing so Flash and Pro placement stays an explicit product decision.
Features teams usually try first
Plain chat and multi-turn history
Start with non-streaming chat and a fixed evaluation set. Confirm:
- system instructions are honored
- multi-turn context is preserved
- truncation with
max_tokensmatches product expectations - refusals and empty responses are handled by your UI
Streaming
Most chat products already consume OpenAI SDK streams. When enabling streaming for Gemini:
- measure time-to-first-token separately from total duration
- decide what the UI shows if the stream ends with an error chunk
- avoid silent failover after tokens have already been rendered
Reliability rules for early-stream versus mid-stream failures belong with your LLM API failover strategy, not ad hoc client retries.
Tool and function calling
Gemini's OpenAI-compatible surface supports tools for many agent workflows:
tools = [
{
"type": "function",
"function": {
"name": "search_docs",
"description": "Search internal engineering docs",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"limit": {"type": "integer"},
},
"required": ["query"],
},
},
}
]
response = client.chat.completions.create(
model="gemini-3.5-flash",
messages=[{"role": "user", "content": "Find runbooks for gateway failover."}],
tools=tools,
tool_choice="auto",
)
Validate tool names against an allowlist and validate arguments with JSON Schema or Pydantic before side effects. Portability details and an incompatibility matrix live in portable tool calling across providers.
Thinking / reasoning effort mapping
Gemini models can expose reasoning configuration through OpenAI-compatible fields. Different Gemini models map reasoning effort differently; check Google's current mapping table when you tune "how hard" the model thinks. A gateway may also accept effort suffixes on model aliases for clients that can only set a model string. Treat those as operator conveniences and document which alias maps to which effort.
Embeddings and adjacent endpoints
Embeddings and media workflows often need separate endpoint checks. Google's OpenAI compatibility page covers chat-oriented samples first; file upload/download compatibility for some batch flows may still require the native Google client. Verify each adjacent surface on its own curl before declaring "the whole OpenAI SDK works."
Limitations and mismatches to budget for
Build an explicit mismatch list for your product, not a vague "test everything" note:
- unsupported or silently dropped OpenAI parameters
- multimodal part encoding differences (especially images and audio)
- tool calling edge cases (parallel calls, empty argument objects, enum drift)
- safety filters that return different refusal shapes than OpenAI
- beta gaps Google still lists under current limitations
- rate-limit and error-header differences
When a request returns 400 on Gemini after succeeding on OpenAI, strip OpenAI-only fields one at a time and log the raw error body. Do not "fix" the failure by catching every exception and retrying another provider mid-stream.
Evaluation plan that avoids false confidence
- Freeze a 20–50 prompt suite with expected tool call names where relevant.
- Run the suite on your current default model and on
gemini-3.5-flash(or your Pro alias). - Score schema validity and required tool invocation separately from prose quality.
- Measure p50/p95 latency and cost per successful task.
- Only then change routing policy. If Gemini wins on latency but loses on schema validity, route only the workflows that tolerate freer text.
curl checks before application code
curl https://your-gateway.example.com/v1/chat/completions \
-H "Authorization: Bearer $GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-flash",
"messages": [
{"role": "user", "content": "Reply with the single word ready."}
]
}'
For a direct Google laptop test, swap the host to https://generativelanguage.googleapis.com/v1beta/openai/ and use a Gemini API key. Prefer the gateway path for shared environments so credentials are not copied into every workstation.
Operating Gemini as a channel
In a multi-provider fleet, Gemini should look like any other channel:
- credentials stored server-side
- model aliases reviewed when Google renames or retires IDs
- rate limits and budgets scoped by team or product
- health and error rates visible next to other providers
- price context reviewed when default routing changes
This is how multi-provider LLM gateways stay operable: Gemini is a routing target, not a second application architecture.
Security and key hygiene
- Never embed Gemini keys in browsers or mobile apps.
- Prefer gateway-scoped keys with least privilege for each service.
- Log tool arguments carefully; agents can send sensitive user data into function calls.
- Review OWASP LLM guidance when agents can call external tools automatically; excessive agency is a product risk, not only a model risk.
Migration sequence
- Enable a Gemini channel and verify
/v1/chat/completionswith curl. - Point one non-critical service at the Gemini model alias.
- Run the evaluation suite and compare schema/tool success rates.
- Add streaming clients.
- Add tool-calling agents with server-side argument validation.
- Decide pinned, canary, or failover placement in routing policy.
- Document the rollback model alias before expanding traffic.
Official sources
- Gemini API OpenAI compatibility
- Gemini API models
- OpenAI Chat Completions reference
- OpenAI function calling guide
Last checked: 2026-07-15. Confirm model IDs, endpoint paths, and limitation tables on Google's docs before locking production routing.
Common questions
Is the Gemini OpenAI-compatible endpoint a full OpenAI clone?
No. It is a compatibility layer for common OpenAI client workflows. Some endpoints, parameters, and behaviors differ. Read Google's limitations section for the features you depend on. Google still recommends the native Gemini API when you are not already invested in OpenAI libraries.
Should I use Gemini through Google's base URL or through a gateway?
Use Google's base URL for quick local experiments. Use a gateway when multiple services, teams, providers, budgets, or audit requirements are involved.
Which Gemini model should I start with?
Start with the current Flash-class ID in Google's docs (as of this writing, samples use gemini-3.5-flash) for latency-sensitive evaluation, and a Pro-class model for harder reasoning or tool workflows. Map those to stable gateway aliases rather than scattering raw IDs through application configs.
Can I keep the same tool schemas I use with OpenAI?
Often yes for initial ports, but validate each schema. Parallel tool calling, strict JSON, and edge-case argument encoding need explicit tests on Gemini.
First Gemini canary
Create or select a Gemini channel, call /v1/chat/completions with the OpenAI SDK against your gateway, and run a fixed 20-prompt evaluation that includes at least one tool call. Compare schema success and latency to your current default, then decide whether Gemini becomes default, canary, or failover for that workflow.