Skip to content

Migrate Providing_base_cases.ipynb to google-genai SDK (#446) - #1406

Open
anushkamishra7 wants to merge 1 commit into
google-gemini:mainfrom
anushkamishra7:sdk-migration-notebook
Open

anushkamishra7 wants to merge 1 commit into
google-gemini:mainfrom
anushkamishra7:sdk-migration-notebook

Conversation

@anushkamishra7

Copy link
Copy Markdown

What

Migrates examples/prompting/Providing_base_cases.ipynb from the legacy google-generativeai SDK to google-genai, per #446. Scope is this one notebook only — both call sites (genai.configure, genai.GenerativeModel) and nothing else.

Why this notebook, and why now

#446 is closed, but the notebook was never migrated. Two earlier attempts exist and were both closed without merging:

No open PR or issue currently claims this notebook. Per CONTRIBUTING.md's guidance that small, well-scoped fixes can go straight to a PR, this skips re-opening #446 and goes directly to review.

Changes

  • %pip install google-generativeai → %pip install "google-genai>=2.9.0"
  • import google.generativeai as genai → from google import genai / from google.genai import types
  • genai.configure(api_key=...) → client = genai.Client(api_key=...)
  • genai.GenerativeModel(model_name=..., system_instruction=...) → a MODEL_ID Colab selector (the standard form used elsewhere in the repo) + types.GenerateContentConfig(system_instruction=...) passed per call
  • model.generate_content(prompt) → client.models.generate_content(model=MODEL_ID, contents=prompt, config=...)

Content and structure are otherwise unchanged: same two system-instruction examples (tourist assistant, library assistant), same four prompts, same on-topic/off-topic and specified/unspecified-genre comparisons. One markdown sentence was lightly reworded ("define the model" → "define a system instruction") since the new API has no persistent model object, and the second example's redundant model-construction cell was removed since MODEL_ID is already defined and reused — the Interactions-API-style notebooks already migrated in this folder (e.g. Role_prompting.ipynb) follow the same one-selector-reused-across-examples shape.

Why client.models.generate_content() instead of client.interactions.create()

The styleguide's default for new text-generation code is the Interactions API, but I hit a reproducible, serious reliability problem with it during testing and backed off to client.models.generate_content() instead:

  • A single client.interactions.create() call took ~70s for a one-line prompt.
  • In a longer test, a call exceeded a 500-second hard timeout.
  • A third attempt hung for over 5 hours. The TCP socket was stuck in CLOSE_WAIT with zero CPU activity — the server had closed its end, but the client was blocked on a read that never returned.
  • I set an explicit client-side timeout (HttpOptions(timeout=...)) and upgraded to the latest SDK (2.27.0) to rule out a stale-version bug. A bounded retry with a 70-second watchdog still hung past the configured 45-second timeout, confirming the hang isn't something a caller can route around.

client.models.generate_content() is the pattern already used by every other already-migrated notebook in examples/prompting/ (e.g. Basic_Classification.ipynb, Basic_Evaluation.ipynb), and it responded reliably in seconds throughout testing. Given the choice between matching the newer-but-currently-broken default and the older-but-stable, already-established pattern in this exact folder, I went with the latter and am flagging the Interactions API issue here rather than silently working around it.

Verification

  • python tools/nbfmt_cli.py examples/prompting/Providing_base_cases.ipynb — pass
  • python tools/nblint_cli.py examples/prompting/Providing_base_cases.ipynb — 0 errors, 2 pre-existing line-length warnings in the unmodified instructions prompt strings (present on main before this change too)
  • python tools/check_readme_links.py examples/prompting/Providing_base_cases.ipynb — linked
  • Ran the notebook live end-to-end on the notebook's default model, gemini-3.8-flash, and saved the real output.

@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

@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 migrates the Providing_base_cases.ipynb notebook to the modern google-genai SDK. The changes focus on updating the library imports, client initialization, and content generation patterns to align with current repository standards while ensuring stable execution.

Highlights

  • SDK Migration: Migrated the notebook from the legacy google-generativeai SDK to the new google-genai SDK (version >= 2.9.0).
  • API Implementation: Updated code to use client.models.generate_content instead of the Interactions API to ensure reliability and consistency with other repository examples.
  • Configuration Updates: Replaced global genai.configure calls with a client-based architecture and updated model selection to use a reusable MODEL_ID Colab selector.
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. ↩

@github-actions github-actions Bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Oct 4, 2026

@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 updates the Providing_base_cases.ipynb notebook to migrate from the deprecated google-generativeai library to the new google-genai SDK (version >=2.9.0). The changes include updating the installation command, adjusting imports, initializing the client using genai.Client(), introducing a MODEL_ID parameter selector, and updating the generation calls to use client.models.generate_content with types.GenerateContentConfig. The notebook outputs have also been updated to reflect the new model's responses. I have no feedback to provide as the changes align with the repository's style guide.

@kkorpal
kkorpal self-requested a review October 5, 2026 12:00
@kkorpal kkorpal self-assigned this Oct 5, 2026

@Giom-V Giom-V left a comment

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.

Hi @anushkamishra7, thanks a lot for tackling this migration!

However, this PR uses client.models.generate_content(), whereas we are now migrating our notebooks to the Interactions API (client.interactions.create() and interaction.output_text).

Could you please update the notebook to use client.interactions.create(model=MODEL_ID, input=..., system_instruction=instructions) and follow the setup structure shown in our Get Started guide?

Here are a couple of links to help you update it:

Thanks again!

@Giom-V Giom-V added status:awaiting response Awaiting a response from the author and removed status:awaiting review PR awaiting review from a maintainer labels Oct 5, 2026
@anushkamishra7

Copy link
Copy Markdown
Author

Thanks for the guidance! Happy to switch to the Interactions API. One thing worth flagging first: during testing, client.interactions.create() hung indefinitely for me — reproduced 3 times, including once for 5+ hours, with the TCP connection sitting in CLOSE_WAIT (server closed its end, client never noticed). This held even after upgrading the SDK (2.25.0 → 2.27.0) and setting an explicit HttpOptions(timeout=90000) — the configured timeout wasn't honored by interactions.create() specifically. client.models.generate_content() never showed this issue in the same testing.

I'll go ahead and migrate to interactions.create() as requested, but wanted to flag this in case it's a known issue, or if there's a recommended pattern to guard against it (e.g. wrapping the call with my own client-side timeout/retry) so the notebook doesn't risk hanging for someone running it fresh in Colab.

@github-actions

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 Oct 20, 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 response Awaiting a response from the author status:stale Issue/PR is marked for closure due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

⌂ Proxy