Add Gemini API Client - #2
Conversation
|
Warning Rate limit exceeded
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 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. 📝 WalkthroughWalkthroughThe Changes
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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.
|
|
||
| import anthropic | ||
| import openai | ||
| from google import genai |
There was a problem hiding this comment.
| 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/"), | ||
| ) |
There was a problem hiding this comment.
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/"),
)There was a problem hiding this comment.
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
| 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/"), | ||
| ) |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
♻️ Duplicate comments (1)
agent/api.py (1)
38-45:⚠️ Potential issue | 🟠 MajorGemini/Google branch is still unreachable from the CLI path.
This branch (Line 38) cannot be selected via CLI because
agent/main.pyLine 185 currently allows only["openai", "claude"], and that value is passed directly intocreate_inference_serveratagent/main.pyLine 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 duplicateGEMINI_API_KEYresolution.
_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.
a71e9e0 to
43f1447
Compare
Add https://ai.google.dev/gemini-api/docs/openai
Usage (
api_type == "gemini"):Summary by CodeRabbit