-
Notifications
You must be signed in to change notification settings - Fork 807
fix(sample-app): Update google genai package #3358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughSwitched Gemini usage from Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant App as Sample App
participant Client as genai.Client
participant Models as Client.models
rect rgba(200,230,255,0.25)
note right of App: predict_text / async_predict_text
App->>Client: Client(api_key)
App->>Models: generate_content(model="gemini-1.5-pro-002", contents="prompt")
Models-->>App: response (text/content)
end
rect rgba(220,255,220,0.25)
note right of App: chat (two-step conversation)
App->>Models: generate_content(model, contents="Hello, how are you?")
Models-->>App: response1
App->>Models: generate_content(model, contents=[{"role":"user","parts":[{"text":"..."}]},{"role":"assistant","parts":[{"text":"response1"}]}, ...])
Models-->>App: response2
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important
Looks good to me! 👍
Reviewed everything up to 46da90d in 1 minute and 26 seconds. Click for details.
- Reviewed
73
lines of code in1
files - Skipped
0
files when reviewing. - Skipped posting
4
draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/sample-app/sample_app/gemini.py:3
- Draft comment:
Verify that the new package name (google.genai) is the correct replacement for google.generativeai. - Reason this comment was not posted:
Confidence changes required:0%
<= threshold50%
None
2. packages/sample-app/sample_app/gemini.py:13
- Draft comment:
Consider verifying that the API key is not None to avoid potential runtime issues. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% While it's true that the API key could be None, this is a common pattern in Python. The code will fail fast with a clear error if the API key is missing. Adding a manual check wouldn't significantly improve the error handling. The comment is asking for defensive programming that isn't strictly necessary. The genai.Client constructor likely has its own validation. The comment does point out a potential runtime issue. Missing environment variables are a common source of problems in production. However, explicit None checks are not idiomatic Python - we prefer EAFP (Easier to Ask for Forgiveness than Permission) over LBYL (Look Before You Leap). The error from the Client constructor will be clear enough. Delete the comment. The suggested change would add unnecessary defensive programming that goes against Python idioms.
3. packages/sample-app/sample_app/gemini.py:32
- Draft comment:
The async function calls a synchronous 'generate_content' method. If the new library supports an async variant, use it to avoid blocking the event loop. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 30% vs. threshold = 50% This is a good catch - the function is marked async but doesn't use any async calls, which defeats the purpose. However, we don't have strong evidence that 'agenerate_content' exists in the new library. The suggestion is speculative without documentation of the new library. The old code used generate_content_async, but we can't assume the new library has similar methods. We don't have documentation for the new google.genai library to confirm if agenerate_content exists. The comment could be misleading if this method doesn't exist. While we can't be certain about the specific method name, the underlying issue - using sync code in an async function - is a real problem that should be addressed. Keep the first part of the comment about the async/sync mismatch but remove the specific suggestion since we can't verify it.
4. packages/sample-app/sample_app/gemini.py:45
- Draft comment:
The variable 'response1' is generated but not used in the chat workflow. Remove it or use it if needed. - Reason this comment was not posted:
Confidence changes required:33%
<= threshold50%
None
Workflow ID: wflow_xd1IGTX7n3zzqub6
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important
Looks good to me! 👍
Reviewed 1f029a9 in 58 seconds. Click for details.
- Reviewed
40
lines of code in1
files - Skipped
0
files when reviewing. - Skipped posting
3
draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/sample-app/sample_app/gemini.py:9
- Draft comment:
Consider checking if 'GENAI_API_KEY' exists. If the env var is missing, the client might not be initialized as intended. - Reason this comment was not posted:
Comment was on unchanged code.
2. packages/sample-app/sample_app/gemini.py:25
- Draft comment:
The async function calls a synchronous 'generate_content'. If an async version exists, consider using it to avoid blocking the event loop. - Reason this comment was not posted:
Comment was not on a location in the diff, so it can't be submitted as a review comment.
3. packages/sample-app/sample_app/gemini.py:36
- Draft comment:
The chat workflow now makes a single generate_content call. If the original intent was to simulate conversation turns, please clarify or reintroduce multiple calls. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%
<= threshold50%
The comment is asking the PR author to clarify their intention regarding the change in the chat workflow. This violates the rule against asking the author to confirm their intention or explain their changes. Therefore, this comment should not be approved.
Workflow ID: wflow_6tCgrLFE6H3YsM9Y
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important
Looks good to me! 👍
Reviewed 7530404 in 44 seconds. Click for details.
- Reviewed
37
lines of code in1
files - Skipped
0
files when reviewing. - Skipped posting
2
draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/sample-app/sample_app/gemini.py:47
- Draft comment:
Ensure that the conversation message schema (role, parts with text) matches the expected format for the google.genai API. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%
<= threshold50%
This comment is asking the author to ensure that the schema matches the expected format for an API. It is not a specific suggestion or a request for a test, but rather a general request for confirmation. This violates the rule against asking the author to ensure behavior is intended.
2. packages/sample-app/sample_app/gemini.py:53
- Draft comment:
Confirm that generate_content accepts a list as the 'contents' parameter, as used for conversation history. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%
<= threshold50%
The comment is asking the author to confirm the type of a parameter, which is not allowed according to the rules. It doesn't provide a specific suggestion or point out a clear issue with the code.
Workflow ID: wflow_QaEJ25pWX0UOwAgT
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/sample-app/sample_app/gemini.py (1)
38-58
: Optional: avoid double request in sample chat.For a lightweight sample, you can send the conversation in one request to cut latency/cost.
Apply:
- # First message - response1 = client.models.generate_content( - model="gemini-1.5-pro-002", - contents="Hello, how are you?", - ) - - # Second message with conversation history - conversation = [ - {"role": "user", "parts": [{"text": "Hello, how are you?"}]}, - {"role": "model", "parts": [{"text": response1.text}]}, - {"role": "user", "parts": [{"text": "What is the capital of France?"}]} - ] - - response2 = client.models.generate_content( - model="gemini-1.5-pro-002", - contents=conversation, - ) - - return response2.text + conversation = [ + {"role": "user", "parts": [{"text": "Hello, how are you?"}]}, + {"role": "user", "parts": [{"text": "What is the capital of France?"}]}, + ] + response = client.models.generate_content( + model="gemini-1.5-pro-002", + contents=conversation, + ) + return response.text
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/sample-app/sample_app/gemini.py
(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.py
: Store API keys only in environment variables/secure vaults; never hardcode secrets in code
Use Flake8 for code linting and adhere to its rules
Files:
packages/sample-app/sample_app/gemini.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
- GitHub Check: Lint
- GitHub Check: Test Packages (3.11)
- GitHub Check: Build Packages (3.11)
- GitHub Check: Test Packages (3.12)
- GitHub Check: Test Packages (3.10)
- GitHub Check: Analyze (actions)
- GitHub Check: Analyze (python)
🔇 Additional comments (2)
packages/sample-app/sample_app/gemini.py (2)
16-19
: LGTM on Client.models.generate_content usage.Call shape and return handling look correct.
48-50
: No change needed: themodel
role is correct per the google-genai SDK (valid roles areuser
andmodel
).
import os | ||
import asyncio | ||
import google.generativeai as genai | ||
import google.genai as genai |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Ensure dependency is added and old one removed.
google.genai
requires the google-genai
package. Verify sample-app deps include it and remove any google-generativeai
to avoid import/runtime conflicts.
Run:
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Deps files:"; fd -a -t f '(requirements.*\.txt|pyproject\.toml|Pipfile(\.lock)?)' || true
echo "Refs to genai:"; rg -n -i 'google-genai|google\.genai|google\.generativeai' -C1 || true
Length of output: 11609
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "Checking pyproject.toml for google-genai and google-generativeai"
rg -n "google-genai" -C2 packages/sample-app/pyproject.toml || true
rg -n "google-generativeai" -C2 packages/sample-app/pyproject.toml || true
Length of output: 642
Add google-genai
and remove google-generativeai
in sample-app deps.
In packages/sample-app/pyproject.toml
under [tool.poetry.dependencies]
, delete
google-generativeai = "^0.8.3"
and add
google-genai = "^1.0.0"
🤖 Prompt for AI Agents
In packages/sample-app/pyproject.toml under [tool.poetry.dependencies] (around
where other app deps are declared), remove the line `google-generativeai =
"^0.8.3"` and add `google-genai = "^1.0.0"` instead; after editing, run your
dependency update (poetry update or poetry lock && poetry install) to apply
changes and verify the existing import in
packages/sample-app/sample_app/gemini.py (`import google.genai as genai`)
remains valid and adjust any API usage if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
Traceloop.init(app_name="gemini_example") | ||
|
||
genai.configure(api_key=os.environ.get("GENAI_API_KEY")) | ||
client = genai.Client(api_key=os.environ.get("GENAI_API_KEY")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fail fast on missing/misnamed API key.
Passing None
to Client(api_key=...)
will yield confusing auth errors. Prefer GOOGLE_API_KEY
(with GENAI_API_KEY
as fallback) and raise a clear message if missing.
Apply:
-client = genai.Client(api_key=os.environ.get("GENAI_API_KEY"))
+API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY")
+if not API_KEY:
+ raise RuntimeError("Missing GOOGLE_API_KEY (preferred) or GENAI_API_KEY in environment.")
+client = genai.Client(api_key=API_KEY)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
client = genai.Client(api_key=os.environ.get("GENAI_API_KEY")) | |
API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY") | |
if not API_KEY: | |
raise RuntimeError("Missing GOOGLE_API_KEY (preferred) or GENAI_API_KEY in environment.") | |
client = genai.Client(api_key=API_KEY) |
🤖 Prompt for AI Agents
In packages/sample-app/sample_app/gemini.py around line 9, the code currently
passes whatever is returned from os.environ.get("GENAI_API_KEY") to genai.Client
which can be None; change this to read os.environ.get("GOOGLE_API_KEY") first
and fall back to os.environ.get("GENAI_API_KEY"), and if neither is set raise a
clear RuntimeError (or SystemExit) with a concise message like "Missing API key:
set GOOGLE_API_KEY or GENAI_API_KEY" so the program fails fast with an
actionable error instead of producing confusing auth errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important
Looks good to me! 👍
Reviewed d958770 in 38 seconds. Click for details.
- Reviewed
35
lines of code in1
files - Skipped
0
files when reviewing. - Skipped posting
2
draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/sample-app/sample_app/gemini.py:38
- Draft comment:
Removed extraneous whitespace between the docstring and first code line for cleaner formatting. - Reason this comment was not posted:
Confidence changes required:0%
<= threshold50%
None
2. packages/sample-app/sample_app/gemini.py:50
- Draft comment:
Added a trailing comma in the conversation list. This is a good practice for cleaner diffs and easier modifications. - Reason this comment was not posted:
Confidence changes required:0%
<= threshold50%
None
Workflow ID: wflow_8c6MRPUz4Iz7Na1x
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (3)
packages/sample-app/sample_app/gemini.py (3)
3-3
: Ensure dependency switch is reflected in sample-app deps.Confirm
google-genai
is added andgoogle-generativeai
removed in sample-app’s pyproject to avoid import/runtime conflicts.#!/bin/bash # Verify dependency switch in sample-app rg -n "google-genai|google-generativeai" packages/sample-app/pyproject.toml -n -C2 || true
9-9
: Fail fast on missing API key and support GOOGLE_API_KEY fallback.Avoid passing None; provide a clear error and prefer GOOGLE_API_KEY.
- client = genai.Client(api_key=os.environ.get("GENAI_API_KEY")) +API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY") +if not API_KEY: + raise RuntimeError("Missing GOOGLE_API_KEY (preferred) or GENAI_API_KEY in environment.") +client = genai.Client(api_key=API_KEY)
28-31
: Async function is using a blocking sync call; switch to AsyncClient.Use AsyncClient and await to avoid blocking the loop.
- response = client.models.generate_content( - model="gemini-1.5-pro-002", - contents="Give me ten interview questions for the role of program manager.", - ) + from google.genai import AsyncClient + async with AsyncClient(api_key=os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY")) as aclient: + response = await aclient.models.generate_content( + model=MODEL, + contents="Give me ten interview questions for the role of program manager.", + )
🧹 Nitpick comments (6)
packages/sample-app/sample_app/gemini.py (6)
7-10
: Avoid import-time side effects; lazily init the client.This improves testability and respects env set after import.
-Traceloop.init(app_name="gemini_example") - -API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY") -if not API_KEY: - raise RuntimeError("Missing GOOGLE_API_KEY (preferred) or GENAI_API_KEY in environment.") -client = genai.Client(api_key=API_KEY) +Traceloop.init(app_name="gemini_example") + +_client = None +def get_client() -> genai.Client: + global _client + if _client is None: + api_key = os.getenv("GOOGLE_API_KEY") or os.getenv("GENAI_API_KEY") + if not api_key: + raise RuntimeError("Missing GOOGLE_API_KEY (preferred) or GENAI_API_KEY in environment.") + _client = genai.Client(api_key=api_key) + return _clientApply the following localized replacements where used:
- response = client.models.generate_content( + response = get_client().models.generate_content(
16-19
: Factor model name; make it configurable.Reduces duplication and eases upgrades.
+MODEL = os.getenv("GEMINI_MODEL", "gemini-1.5-pro-002") - response = client.models.generate_content( - model="gemini-1.5-pro-002", + response = get_client().models.generate_content( + model=MODEL, contents="Give me ten interview questions for the role of program manager.", )
40-44
: Defensive handling for potential empty text.Responses can occasionally lack
.text
. Guard and surface a helpful error.- response1 = client.models.generate_content( - model="gemini-1.5-pro-002", + response1 = get_client().models.generate_content( + model=MODEL, contents="Hello, how are you?", ) + if not getattr(response1, "text", None): + raise RuntimeError(f"Empty response text from model {MODEL}: {response1}")
53-56
: Optional: pass generation config explicitly.Expose temperature/top_p to make the sample deterministic or tunable.
- response2 = client.models.generate_content( - model="gemini-1.5-pro-002", - contents=conversation, - ) + response2 = get_client().models.generate_content( + model=MODEL, + contents=conversation, + # generation_config={"temperature": 0.2, "top_p": 0.95}, # optional + )
58-58
: Mirror the defensive check on the final response.Avoid returning None.
- return response2.text + if not getattr(response2, "text", None): + raise RuntimeError(f"Empty response text from model {MODEL}: {response2}") + return response2.text
46-51
: Conversation schema valid; improve response parsing
- Payload uses
contents
and roles"user"
/"model"
per google-genai 1.x—no changes needed.- Accessing
response1.text
isn’t guaranteed for non-text responses; useresponse1.candidates[0].content
or inspectresponse1.result.parts
to handle function-call–only or other multi-part responses robustly.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/sample-app/sample_app/gemini.py
(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.py
: Store API keys only in environment variables/secure vaults; never hardcode secrets in code
Use Flake8 for code linting and adhere to its rules
Files:
packages/sample-app/sample_app/gemini.py
🧬 Code graph analysis (1)
packages/sample-app/sample_app/gemini.py (2)
packages/traceloop-sdk/traceloop/sdk/__init__.py (2)
Traceloop
(37-275)init
(49-206)packages/traceloop-sdk/traceloop/sdk/decorators/__init__.py (1)
workflow
(33-47)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: Build Packages (3.11)
- GitHub Check: Test Packages (3.10)
- GitHub Check: Test Packages (3.12)
- GitHub Check: Test Packages (3.11)
- GitHub Check: Lint
🔇 Additional comments (1)
packages/sample-app/sample_app/gemini.py (1)
38-38
: Docstring update LGTM.
feat(instrumentation): ...
orfix(instrumentation): ...
.Important
Refactor
gemini.py
to usegenai.Client
, remove async paths, and enhance chat with conversation context.genai.Client
for AI operations ingemini.py
.async_predict_text()
now uses synchronousclient.models.generate_content()
.chat()
to use conversation context for coherent multi-turn replies.google.generativeai
togoogle.genai
.This description was created by
for d958770. You can customize this summary. It will automatically update as commits are pushed.
Summary by CodeRabbit
New Features
Refactor
Documentation