Last verified: July 20, 2026
You can call Gemini from Python through LumeAPI with the OpenAI client, one LumeAPI key, and an exact Gemini model ID. Set base_url to https://api.lumeapi.site/v1, start with gemini-3.5-flash for responsive text workloads, and switch to a Pro model only after your own evaluation shows a benefit.
This route is useful when the same application also calls GPT or Claude: the client, key, wallet, and operational logs stay consistent while the model ID changes. Google's native Gen AI SDK remains a separate option for applications that require native Gemini features outside the compatible surface.
Choose a Gemini model
The LumeAPI catalog showed these text models on July 20, 2026. Rates are USD per 1 million input/output tokens.
| Model ID | LumeAPI input / output | Page reference input / output | Difference shown | Use when |
|---|---|---|---|---|
gemini-3-flash | $0.30 / $1.80 | $0.50 / $3.00 | 40% lower | Lightweight generation and classification |
gemini-3.5-flash | $0.90 / $5.40 | $1.50 / $9.00 | 40% lower | Interactive applications and higher-QPS text traffic |
gemini-3.1-pro-preview | $1.20 / $7.20 | $2.00 / $12.00 | 40% lower | Harder or longer-context work after testing preview risk |
Choose on task success, latency, and cost per completed task. A preview model also needs a change-management plan because availability or behavior can change. Check the live Gemini API page before publishing a fixed rate or model ID in application documentation.
Install the SDK used by this route
This guide uses the OpenAI Python package because LumeAPI exposes Gemini through an OpenAI-compatible Chat Completions endpoint:
python -m pip install --upgrade openaiSet a LumeAPI key outside the source file:
export LUMEAPI_KEY='your-lumeapi-key'PowerShell:
$env:LUMEAPI_KEY = 'your-lumeapi-key'A Google Gemini key is not a LumeAPI key. Always match the key to the base URL that issued it.
Send a Gemini request
Create gemini_example.py:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['LUMEAPI_KEY'],
base_url='https://api.lumeapi.site/v1',
)
response = client.chat.completions.create(
model='gemini-3.5-flash',
messages=[
{
'role': 'system',
'content': 'Answer directly and include one practical example.',
},
{
'role': 'user',
'content': 'Explain exponential backoff for an API client.',
},
],
)
print(response.choices[0].message.content)
print({
'model': response.model,
'input_tokens': response.usage.prompt_tokens if response.usage else None,
'output_tokens': response.usage.completion_tokens if response.usage else None,
})Run it with python gemini_example.py. The request goes to the shared LumeAPI Chat Completions route, and text is returned through the familiar choices structure.
Stream Gemini output
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ['LUMEAPI_KEY'],
base_url='https://api.lumeapi.site/v1',
)
stream = client.chat.completions.create(
model=os.getenv('LUMEAPI_MODEL', 'gemini-3.5-flash'),
messages=[
{'role': 'user', 'content': 'Write a five-point API launch checklist.'}
],
stream=True,
)
for chunk in stream:
if not chunk.choices:
continue
text = chunk.choices[0].delta.content
if text:
print(text, end='', flush=True)
print()Streaming must be tested through every proxy and framework in the request path. Handle empty events, client disconnects, total timeout, and partial output explicitly.
Estimate Gemini 3.5 Flash cost
For 2,000 input tokens and 500 output tokens at the rates above:
2,000 / 1,000,000 × $0.90 = $0.0018
500 / 1,000,000 × $5.40 = $0.0027
total $0.0045At the page's $1.50 / $9.00 reference rates, the same usage totals $0.0075. The difference is 40%. This does not mean every LumeAPI model has the same discount; supported GPT models on the site can reach 70%, while the Gemini models in this table show 40% against their displayed references.
def gemini_flash_cost(input_tokens: int, output_tokens: int) -> float:
return (
input_tokens / 1_000_000 * 0.90
+ output_tokens / 1_000_000 * 5.40
)
print(f'${gemini_flash_cost(2000, 500):.4f}')Use the calculator for forecasting. Review the LumeAPI USD wallet and per-call logs for actual model, token, cost, and latency data.
Switch between Flash and Pro safely
Keep the external model ID behind an application alias:
import os
from openai import OpenAI
GEMINI_MODELS = {
'fast': 'gemini-3.5-flash',
'economy': 'gemini-3-flash',
'pro': 'gemini-3.1-pro-preview',
}
client = OpenAI(
api_key=os.environ['LUMEAPI_KEY'],
base_url='https://api.lumeapi.site/v1',
)
def ask_gemini(question: str, tier: str = 'fast') -> str:
model = GEMINI_MODELS[tier]
response = client.chat.completions.create(
model=model,
messages=[{'role': 'user', 'content': question}],
)
return response.choices[0].message.content or ''Do not route every difficult-looking prompt to Pro without measurement. Build a representative evaluation set and compare task success, latency, output length, retries, and cost.
OpenAI-compatible route versus Google's native SDK
Google's official documentation supports using OpenAI libraries with Gemini by changing client configuration. Google also provides the native google-genai SDK, whose methods and feature surface differ from Chat Completions.
| Need | LumeAPI compatible route | Native Google SDK |
|---|---|---|
| Reuse an existing OpenAI client | Good fit | Requires SDK integration |
| Use one key for GPT, Claude, and Gemini through LumeAPI | Yes | No; provider-specific |
| Consolidate model spend in one LumeAPI wallet | Yes | No; provider billing |
| Provider-native Gemini features | Only when documented on the gateway | Native interface is the reference |
| Text Chat Completions | Documented LumeAPI route | Native generate/interaction methods |
Choose based on the feature contract, not just installation convenience. If a native feature is essential, verify that LumeAPI explicitly supports it before assuming compatibility.
Handle API failures
import os
import openai
from openai import OpenAI
client = OpenAI(
api_key=os.environ['LUMEAPI_KEY'],
base_url='https://api.lumeapi.site/v1',
timeout=30.0,
max_retries=2,
)
def generate(question: str) -> str:
try:
response = client.chat.completions.create(
model='gemini-3.5-flash',
messages=[{'role': 'user', 'content': question}],
)
return response.choices[0].message.content or ''
except openai.AuthenticationError as exc:
raise RuntimeError('Check the LumeAPI key and endpoint.') from exc
except openai.RateLimitError as exc:
raise RuntimeError('Rate limited; queue or retry with bounded backoff.') from exc
except openai.APITimeoutError as exc:
raise RuntimeError('Gemini request timed out.') from exc
except openai.APIStatusError as exc:
raise RuntimeError(
f'HTTP {exc.status_code}; request_id={exc.request_id}'
) from excA 429 may represent account, model, or upstream capacity constraints. Preserve the request ID and inspect logs before changing concurrency. For 401, do not retry; verify the host, key source, and authorization header.
Common Gemini Python problems
The code uses the wrong key
A Google key belongs with Google's endpoint. The code above uses a LumeAPI key because the base URL is api.lumeapi.site.
The model name is rejected
Copy gemini-3.5-flash from the current model docs. Do not assume a display name or preview alias is accepted.
A provider-native parameter is ignored or rejected
The shared Chat Completions shape does not guarantee every native Gemini parameter. Check the model documentation and test the exact field.
The bill is larger than the prompt suggests
Include system messages, conversation history, retrieved context, output tokens, and retries. Use per-call logs instead of estimating from visible user text.
Why LumeAPI is relevant to this task
The immediate benefit is not a generic claim about Gemini. It is operational consolidation: the same Python client, LumeAPI key, USD wallet, and log workflow can call available GPT, Claude, and Gemini models. The same account also covers listed image and video models, although those modalities may use different request and job patterns.
This makes model experiments easier, but the application still owns evaluation, routing decisions, input validation, output validation, and safe rollback.
Sources and verification
- LumeAPI Gemini API and Gemini 3.5 Flash docs for current model IDs, rates, and the compatible endpoint verified July 20, 2026.
- Google's OpenAI compatibility guide for the official OpenAI-library pattern.
- Official Google Gen AI Python SDK for the native SDK distinction.
- Official OpenAI Python library for client, streaming, timeout, retry, usage, and error conventions.
The Python code was syntax-checked on July 20, 2026. No customer inference key was used, so this article does not claim observed response quality, latency, or native-feature parity.