← Back to research
Guides11 min readPublished 2026-07-20

Gemini API in Python: OpenAI-Compatible Example with LumeAPI

Call Gemini 3.5 Flash from Python through LumeAPI. Stream output, compare Flash and Pro, calculate token cost, and handle common API failures in production.

By LumeAPI Engineering Team

Gemini API hub → Explore Lower-Cost Gemini Access →

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 IDLumeAPI input / outputPage reference input / outputDifference shownUse when
gemini-3-flash$0.30 / $1.80$0.50 / $3.0040% lowerLightweight generation and classification
gemini-3.5-flash$0.90 / $5.40$1.50 / $9.0040% lowerInteractive applications and higher-QPS text traffic
gemini-3.1-pro-preview$1.20 / $7.20$2.00 / $12.0040% lowerHarder 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:

bash
python -m pip install --upgrade openai

Set a LumeAPI key outside the source file:

bash
export LUMEAPI_KEY='your-lumeapi-key'

PowerShell:

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:

python
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

python
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:

text
2,000 / 1,000,000 × $0.90 = $0.0018
  500 / 1,000,000 × $5.40 = $0.0027
total                            $0.0045

At 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.

python
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:

python
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.

NeedLumeAPI compatible routeNative Google SDK
Reuse an existing OpenAI clientGood fitRequires SDK integration
Use one key for GPT, Claude, and Gemini through LumeAPIYesNo; provider-specific
Consolidate model spend in one LumeAPI walletYesNo; provider billing
Provider-native Gemini featuresOnly when documented on the gatewayNative interface is the reference
Text Chat CompletionsDocumented LumeAPI routeNative 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

python
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 exc

A 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

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.