Skip to content

Add Gemini API Client - #2

Open
austin362667 wants to merge 3 commits into
flashinfer-ai:mainfrom
austin362667:austin/add-gemini-api-client
Open

Add Gemini API Client#2
austin362667 wants to merge 3 commits into
flashinfer-ai:mainfrom
austin362667:austin/add-gemini-api-client

Conversation

@austin362667

@austin362667 austin362667 commented Apr 5, 2026

Copy link
Copy Markdown

Add https://ai.google.dev/gemini-api/docs/openai

Usage (api_type == "gemini" ):

client = OpenAI(
    api_key="GEMINI_API_KEY",
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)

Summary by CodeRabbit

  • New Features
    • Added support for Google Gemini as an API option for inference servers. Users can now choose the Gemini/google provider, supply credentials via environment configuration, and optionally specify a custom endpoint URL. This integration is compatible with OpenAI-style interfaces, enabling straightforward use alongside existing OpenAI and Anthropic/Claude options.

@coderabbitai

coderabbitai Bot commented Apr 5, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@austin362667 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 13 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 19 minutes and 13 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 54c6a857-ee2b-4fe8-b5cb-4bd6e0213734

📥 Commits

Reviewing files that changed from the base of the PR and between 84dc076 and 43f1447.

📒 Files selected for processing (1)
  • agent/main.py
📝 Walkthrough

Walkthrough

The create_inference_server function now accepts api_type values "gemini" and "google". For those types it requires GEMINI_API_KEY (from environment) and constructs an OpenAI-compatible openai.OpenAI client, using GEMINI_BASE_URL or a Google Generative Language default base URL.

Changes

Cohort / File(s) Summary
Gemini / Google API support
agent/api.py
Added branching in create_inference_server to handle api_type "gemini" and "google": load GEMINI_API_KEY from env (via _require_env) and instantiate an openai.OpenAI client with base_url set to GEMINI_BASE_URL or https://generativelanguage.googleapis.com/v1beta/openai/. Existing "openai" and "claude"/"anthropic" flows unchanged.

Sequence Diagram(s)

sequenceDiagram
    rect rgba(200,230,255,0.5)
    participant Client
    end
    rect rgba(200,255,200,0.5)
    participant create_inference_server as ServerFactory
    end
    rect rgba(255,240,200,0.5)
    participant Env as Environment
    end
    rect rgba(255,200,200,0.5)
    participant OpenAI as OpenAIClient
    end

    Client->>ServerFactory: request create_inference_server(api_type="gemini"|"google")
    ServerFactory->>Env: _require_env("GEMINI_API_KEY")
    Env-->>ServerFactory: GEMINI_API_KEY (or error)
    ServerFactory->>OpenAI: instantiate openai.OpenAI(base_url=GEMINI_BASE_URL or default)
    ServerFactory-->>Client: return OpenAIClient instance
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A nibble of code beneath moonlight,
GEMINI key found, the routes take flight.
Clients connect, a gentle hum—
OpenAI guise, new engines come.
Hop, infer, and code delights! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding Gemini API client support to the inference server through new api_type branches.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for Gemini and Google API types to the inference server using an OpenAI-compatible endpoint. The review feedback highlights an unused and potentially missing import of the google-genai package, as well as redundant logic for retrieving the Gemini API key that can be simplified using existing helper functions.

Comment thread agent/api.py Outdated

import anthropic
import openai
from google import genai

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The import from google import genai is unused. The implementation uses the openai library to interact with Gemini via its OpenAI-compatible endpoint. Furthermore, google-genai is not listed in requirements.txt, which could lead to an ImportError when this module is loaded.

Comment thread agent/api.py
Comment on lines +40 to +46
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
_require_env(["GEMINI_API_KEY"], api_type)
return openai.OpenAI(
api_key=_require_env(["GEMINI_API_KEY"], api_type),
base_url=os.environ.get("GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/"),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for checking and retrieving GEMINI_API_KEY is redundant. The _require_env function already handles checking for the environment variable and raising a RuntimeError if it's missing, while returning the value if it exists. The current implementation performs manual checks and calls the helper twice unnecessarily.

        return openai.OpenAI(
            api_key=_require_env(["GEMINI_API_KEY"], api_type),
            base_url=os.environ.get("GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/"),
        )

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
agent/api.py (1)

40-45: Consolidate Gemini API key resolution to a single lookup.

Line 40, Line 42, and Line 44 currently re-check the same env var. Resolve once and reuse to keep this branch cleaner and easier to maintain.

Refactor suggestion
     elif api_type in ("gemini", "google"):
-        api_key = os.environ.get("GEMINI_API_KEY")
-        if not api_key:
-            _require_env(["GEMINI_API_KEY"], api_type)
+        api_key = _require_env(["GEMINI_API_KEY"], api_type)
         return openai.OpenAI(
-            api_key=_require_env(["GEMINI_API_KEY"], api_type),
+            api_key=api_key,
             base_url=os.environ.get("GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/"),
         )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@agent/api.py` around lines 40 - 45, The code redundantly re-reads
GEMINI_API_KEY multiple times; capture it once into the api_key variable and
reuse it when constructing the OpenAI client instead of calling
_require_env(["GEMINI_API_KEY"], api_type) again. Update the branch around
api_key, _require_env and the openai.OpenAI(...) call so api_key is passed to
openai.OpenAI and only call _require_env once when needed; keep GEMINI_BASE_URL
lookup unchanged. Ensure references to api_key, _require_env, and openai.OpenAI
are the only symbols modified.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@agent/api.py`:
- Around line 39-46: The Gemini/Google branch in create_inference_server is
unreachable because the CLI's --api_type choices (in main's argument parsing and
where parse_args() value is forwarded) only allow "openai" and "claude"; update
the CLI to include "gemini" and "google" in the choices for --api_type (and any
related validation or help text) so the value passed into
create_inference_server can reach the gemini/google branch; ensure the same
choices array used at argument definition and any subsequent validation/coercion
include "gemini" and "google".

---

Nitpick comments:
In `@agent/api.py`:
- Around line 40-45: The code redundantly re-reads GEMINI_API_KEY multiple
times; capture it once into the api_key variable and reuse it when constructing
the OpenAI client instead of calling _require_env(["GEMINI_API_KEY"], api_type)
again. Update the branch around api_key, _require_env and the openai.OpenAI(...)
call so api_key is passed to openai.OpenAI and only call _require_env once when
needed; keep GEMINI_BASE_URL lookup unchanged. Ensure references to api_key,
_require_env, and openai.OpenAI are the only symbols modified.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2cd89f45-7333-4106-ad51-5770806fe406

📥 Commits

Reviewing files that changed from the base of the PR and between 780e887 and 7dc4d6b.

📒 Files selected for processing (1)
  • agent/api.py

Comment thread agent/api.py
Comment on lines +39 to +46
elif api_type in ("gemini", "google"):
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
_require_env(["GEMINI_API_KEY"], api_type)
return openai.OpenAI(
api_key=_require_env(["GEMINI_API_KEY"], api_type),
base_url=os.environ.get("GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/"),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Gemini branch is not reachable from the current CLI flow.

create_inference_server now supports "gemini"/"google" here, but agent/main.py Line 185 restricts --api_type choices to ["openai", "claude"], and Line 109 passes that value directly. This makes the new branch effectively unusable from CLI.

Suggested follow-up patch (outside this file)
--- a/agent/main.py
+++ b/agent/main.py
@@
 parser.add_argument(
-        "--api_type", type=str, default="openai", choices=["openai", "claude"]
+        "--api_type",
+        type=str,
+        default="openai",
+        choices=["openai", "claude", "anthropic", "gemini", "google"],
 )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@agent/api.py` around lines 39 - 46, The Gemini/Google branch in
create_inference_server is unreachable because the CLI's --api_type choices (in
main's argument parsing and where parse_args() value is forwarded) only allow
"openai" and "claude"; update the CLI to include "gemini" and "google" in the
choices for --api_type (and any related validation or help text) so the value
passed into create_inference_server can reach the gemini/google branch; ensure
the same choices array used at argument definition and any subsequent
validation/coercion include "gemini" and "google".

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
agent/api.py (1)

38-45: ⚠️ Potential issue | 🟠 Major

Gemini/Google branch is still unreachable from the CLI path.

This branch (Line 38) cannot be selected via CLI because agent/main.py Line 185 currently allows only ["openai", "claude"], and that value is passed directly into create_inference_server at agent/main.py Line 109.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@agent/api.py` around lines 38 - 45, The Gemini/Google branch in agent/api.py
is unreachable because the CLI in agent/main.py restricts allowed api_type
values to ["openai", "claude"]; update agent/main.py so the CLI option (and any
validation around create_inference_server) includes "gemini" (and/or "google" if
you expect that alias) or map the CLI value to "gemini" before calling
create_inference_server; specifically modify the CLI choices/default near the
option definition and the value passed into create_inference_server (referenced
symbol: create_inference_server) so that api_type can be "gemini" and the branch
in agent/api.py (api_type in ("gemini", "google")) becomes reachable.
🧹 Nitpick comments (1)
agent/api.py (1)

39-44: Remove duplicate GEMINI_API_KEY resolution.

_require_env(["GEMINI_API_KEY"], api_type) is called twice in the same block. Resolve once and reuse for clarity.

♻️ Proposed refactor
     elif api_type in ("gemini", "google"):
-        api_key = os.environ.get("GEMINI_API_KEY")
-        if not api_key:
-            _require_env(["GEMINI_API_KEY"], api_type)
+        api_key = _require_env(["GEMINI_API_KEY"], api_type)
         return openai.OpenAI(
-            api_key=_require_env(["GEMINI_API_KEY"], api_type),
+            api_key=api_key,
             base_url=os.environ.get("GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/"),
         )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@agent/api.py` around lines 39 - 44, The code calls
_require_env(["GEMINI_API_KEY"], api_type) twice; instead, resolve the key once
into the local variable api_key (already declared) and reuse it for the
openai.OpenAI constructor and any checks. Update the block around the api_key
variable and the openai.OpenAI call so api_key is assigned from
_require_env(...) only when missing and then passed into
openai.OpenAI(api_key=api_key, base_url=...), removing the duplicate
_require_env call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@agent/api.py`:
- Around line 38-45: The Gemini/Google branch in agent/api.py is unreachable
because the CLI in agent/main.py restricts allowed api_type values to ["openai",
"claude"]; update agent/main.py so the CLI option (and any validation around
create_inference_server) includes "gemini" (and/or "google" if you expect that
alias) or map the CLI value to "gemini" before calling create_inference_server;
specifically modify the CLI choices/default near the option definition and the
value passed into create_inference_server (referenced symbol:
create_inference_server) so that api_type can be "gemini" and the branch in
agent/api.py (api_type in ("gemini", "google")) becomes reachable.

---

Nitpick comments:
In `@agent/api.py`:
- Around line 39-44: The code calls _require_env(["GEMINI_API_KEY"], api_type)
twice; instead, resolve the key once into the local variable api_key (already
declared) and reuse it for the openai.OpenAI constructor and any checks. Update
the block around the api_key variable and the openai.OpenAI call so api_key is
assigned from _require_env(...) only when missing and then passed into
openai.OpenAI(api_key=api_key, base_url=...), removing the duplicate
_require_env call.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7619d187-9a33-49c7-8e1a-470899fe2c65

📥 Commits

Reviewing files that changed from the base of the PR and between 7dc4d6b and 84dc076.

📒 Files selected for processing (1)
  • agent/api.py

@austin362667
austin362667 force-pushed the austin/add-gemini-api-client branch from a71e9e0 to 43f1447 Compare April 5, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant