fix: migrate 2 legacy notebooks from google-generativeai to google-genai SDK#1266
Conversation
…i SDK Migrate two notebooks that were still using the deprecated google-generativeai SDK to the new google-genai SDK (google.genai). Changes made to examples/prompting/Providing_base_cases.ipynb: - Replace %pip install google-generativeai with google-genai - Replace import google.generativeai as genai with import google.genai as genai - Replace genai.configure(api_key=...) with genai.Client(api_key=...) - Replace genai.GenerativeModel(...) with client.models.generate_content(...) - Add Colab form model selector (MODEL_ID) for easier model switching - Pass system_instruction via genai.types.GenerateContentConfig Changes made to examples/Search_Wikipedia_using_ReAct.ipynb: - Replace %pip install google-generativeai with google-genai - Replace import google.generativeai as genai with import google.genai as genai - Replace genai.configure(api_key=...) with genai.Client(api_key=...) - Replace genai.GenerativeModel + start_chat() with stateless client.models.generate_content() using explicit chat_history list - Update GenerationConfig references to genai.types.GenerateContentConfig - Update doc links from generativeai to genai API reference - Fix typo in source: 'Retun' -> 'Return'
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request updates two legacy notebooks to utilize the modern google-genai SDK. These changes ensure consistency across the cookbook repository by adopting the latest client-based API patterns, improving maintainability and model interaction logic. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request migrates the Search_Wikipedia_using_ReAct.ipynb and Providing_base_cases.ipynb notebooks to the new google-genai SDK. The feedback focuses on aligning the migration with the repository style guide and optimizing performance. Specifically, the reviewer recommends using the -U flag and minimum version constraints for %pip install commands, removing the unused wikipedia-api package, and using a Colab form selector for model selection. Additionally, the feedback identifies a functional regression in the ReAct class where several generation parameters are silently discarded, and suggests performance improvements by caching Wikipedia page retrievals to avoid redundant network requests.
Search_Wikipedia_using_ReAct.ipynb: - Use **generation_kwargs directly in GenerateContentConfig to preserve all config parameters (max_output_tokens, top_p, top_k, etc.) instead of silently discarding them - Remove unused wikipedia-api package from pip install; add -U flag and version constraint 'google-genai>=1.0.0' - Remove duplicate standalone %pip install wikipedia cell - search(): fetch wikipedia.page() once and reuse .summary and .url from the cached page, eliminating duplicate network requests - lookup(): reuse self._current_page cached during search() instead of re-fetching the page; avoids redundant Wikipedia API call - Add MODEL_ID Colab form selector for easier model switching per style guide Providing_base_cases.ipynb: - Add version constraint to pip install: 'google-genai>=1.0.0'
|
Hi @NaveenKumarG-dev , thanks for your contribution. However, the checks are failing. The Notebook Format check is failing on files you worked. Could you please resolve that check? Here are the steps to fix notebook format failures: Install the tool (if not already) Run the formatter — this auto-fixes most issues Thanks..! |
|
Thanks for the heads-up! I'll fix the notebook formatting issues and rerun the checks. |
| "outputs": [], | ||
| "source": [ | ||
| "%pip install -U -q \"google-generativeai>=0.7.2\"" | ||
| "%%pip install -U -q 'google-genai>=1.0.0'" |
| "outputs": [], | ||
| "source": [ | ||
| "model = genai.GenerativeModel(model_name='gemini-2.0-flash', system_instruction=instructions)" | ||
| "MODEL_ID = \"gemini-2.0-flash\" # @param [\"gemini-2.0-flash\", \"gemini-2.5-flash\", \"gemini-2.5-pro\"] {\"allow-input\": true, isTemplate: true}" |
There was a problem hiding this comment.
Please use with latest available models..
MODEL_ID = "gemini-3.5-flash" # @param ["gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.5-flash-preview", "gemini-3.1-flash-lite", "gemini-3.1-pro-preview"] {"allow-input":true, isTemplate: true}
|
Marking this pull request as stale since it has been open for 14 days with no activity. This PR will be closed if no further activity occurs. |
Summary
This PR migrates two notebooks that were still using the deprecated
google-generativeaiSDK to the newgoogle-genaiSDK, keeping them consistent with the rest of the cookbook.Notebooks Updated
examples/prompting/Providing_base_cases.ipynb%pip install google-generativeai%pip install google-genaiimport google.generativeai as genaiimport google.genai as genaigenai.configure(api_key=...)client = genai.Client(api_key=...)genai.GenerativeModel(model_name=..., system_instruction=...)client.models.generate_content(model=MODEL_ID, config=genai.types.GenerateContentConfig(...))MODEL_IDselector for easy model switchingexamples/Search_Wikipedia_using_ReAct.ipynb%pip install google-generativeai%pip install google-genaiimport google.generativeai as genaiimport google.genai as genaigenai.configure(api_key=...)client = genai.Client(api_key=...)genai.GenerativeModel+start_chat()+chat.send_message()client.models.generate_content()with explicitchat_historylistgenai.GenerativeModel.GenerationConfigdoc linkgenai.types.GenerateContentConfigdoc link'Retun the first 2 or 3 sentences''Return the first 2 or 3 sentences'Testing
Both notebooks have been reviewed cell-by-cell to ensure all
google.generativeaireferences in code cells are fully replaced. The logic and output of each notebook remain unchanged.Colab review link (Providing_base_cases):
https://colab.research.google.com/github/NaveenKumarG-dev/cookbook/blob/fix/migrate-legacy-notebooks-to-google-genai-sdk/examples/prompting/Providing_base_cases.ipynb
Colab review link (Search_Wikipedia_using_ReAct):
https://colab.research.google.com/github/NaveenKumarG-dev/cookbook/blob/fix/migrate-legacy-notebooks-to-google-genai-sdk/examples/Search_Wikipedia_using_ReAct.ipynb