Skip to content

fix: migrate 2 legacy notebooks from google-generativeai to google-genai SDK#1266

Open
NaveenKumarG-dev wants to merge 6 commits into
google-gemini:mainfrom
NaveenKumarG-dev:fix/migrate-legacy-notebooks-to-google-genai-sdk
Open

fix: migrate 2 legacy notebooks from google-generativeai to google-genai SDK#1266
NaveenKumarG-dev wants to merge 6 commits into
google-gemini:mainfrom
NaveenKumarG-dev:fix/migrate-legacy-notebooks-to-google-genai-sdk

Conversation

@NaveenKumarG-dev

Copy link
Copy Markdown
Contributor

Summary

This PR migrates two notebooks that were still using the deprecated google-generativeai SDK to the new google-genai SDK, keeping them consistent with the rest of the cookbook.

Notebooks Updated

examples/prompting/Providing_base_cases.ipynb

Before After
%pip install google-generativeai %pip install google-genai
import google.generativeai as genai import google.genai as genai
genai.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(...))
Hardcoded model name per call Colab form MODEL_ID selector for easy model switching

examples/Search_Wikipedia_using_ReAct.ipynb

Before After
%pip install google-generativeai %pip install google-genai
import google.generativeai as genai import google.genai as genai
genai.configure(api_key=...) client = genai.Client(api_key=...)
genai.GenerativeModel + start_chat() + chat.send_message() Stateless client.models.generate_content() with explicit chat_history list
genai.GenerativeModel.GenerationConfig doc link genai.types.GenerateContentConfig doc link
Typo: 'Retun the first 2 or 3 sentences' Fixed: 'Return the first 2 or 3 sentences'

Testing

Both notebooks have been reviewed cell-by-cell to ensure all google.generativeai references 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

…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'
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions Bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Jun 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • SDK Migration: Migrated two legacy notebooks from the deprecated google-generativeai SDK to the new google-genai SDK.
  • Code Modernization: Updated model initialization and generation patterns to use the new client-based API, including improved chat handling and model selection capabilities.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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

Copy link
Copy Markdown
Contributor

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

Comment thread examples/Search_Wikipedia_using_ReAct.ipynb Outdated
Comment thread examples/Search_Wikipedia_using_ReAct.ipynb Outdated
Comment thread examples/prompting/Providing_base_cases.ipynb Outdated
Comment thread examples/Search_Wikipedia_using_ReAct.ipynb Outdated
Comment thread examples/Search_Wikipedia_using_ReAct.ipynb Outdated
Comment thread examples/Search_Wikipedia_using_ReAct.ipynb Outdated
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'
@kkorpal

kkorpal commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

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)
pip3 install -U git+https://github.com/tensorflow/docs

Run the formatter — this auto-fixes most issues
python3 -m tensorflow_docs.tools.nbfmt quickstarts/YourNotebook.ipynb

Thanks..!

@kkorpal kkorpal added status:awaiting response Awaiting a response from the author and removed status:awaiting review PR awaiting review from a maintainer labels Jun 15, 2026
@kkorpal kkorpal self-assigned this Jun 15, 2026
@NaveenKumarG-dev

Copy link
Copy Markdown
Contributor Author

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'"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please remove extra "%"

"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}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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}

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the status:stale Issue/PR is marked for closure due to inactivity label Jul 3, 2026
@github-actions github-actions Bot removed the status:stale Issue/PR is marked for closure due to inactivity label Jul 6, 2026
@kkorpal kkorpal added status:awaiting review PR awaiting review from a maintainer and removed status:awaiting response Awaiting a response from the author labels Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:examples Issues/PR referencing examples folder status:awaiting review PR awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants