Skip to content

feat: Add "Handling Errors and Retries" quickstart notebook (Resolves #469)#1160

Open
kkorpal wants to merge 9 commits into
mainfrom
feature/retry-logic-backoff
Open

feat: Add "Handling Errors and Retries" quickstart notebook (Resolves #469)#1160
kkorpal wants to merge 9 commits into
mainfrom
feature/retry-logic-backoff

Conversation

@kkorpal
Copy link
Copy Markdown
Collaborator

@kkorpal kkorpal commented Mar 17, 2026

…esolves #469)

@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 introduces a valuable new quickstart guide focused on building more resilient applications by effectively handling API errors and implementing retry mechanisms. It provides practical examples of how to configure the Gemini API client to automatically manage transient network issues and rate limiting, thereby improving the stability and reliability of integrations.

Highlights

  • New Quickstart Notebook: Added a new quickstart notebook titled "Handling Errors and Retries" to demonstrate how to implement robust error handling and retry logic with exponential backoff when using the Gemini API.
  • SDK Retry Features: The new notebook showcases the native HttpRetryOptions configuration within the google-genai SDK, allowing users to easily configure automatic retries for transient API errors like rate limits (429) and server errors (5xx).
  • Documentation Update: The quickstarts/README.md file was updated to include a link and description for the newly added "Handling Errors and Retries" quickstart.
Changelog
  • quickstarts/Handling_Errors_and_Retries.ipynb
    • Added a new Jupyter notebook demonstrating error handling and retry logic for the Gemini API.
  • quickstarts/README.md
    • Updated the quickstarts README to include an entry for the new 'Handling Errors and Retries' notebook.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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:quickstarts Issues/PR referencing quickstarts folder labels Mar 17, 2026
Copy link
Copy Markdown
Contributor

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

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 introduces a new quickstart notebook, Handling_Errors_and_Retries.ipynb, which explains how to use the retry logic in the Gemini API. The notebook is a valuable addition and clearly explains the concept. My review feedback primarily focuses on ensuring the new notebook adheres to the repository's detailed style guide. I've suggested changes related to notebook formatting, command usage, import placement, and code cell structure to align with the established conventions. The accompanying update to the README.md is correct.

"cells": [
{
"cell_type": "code",
"execution_count": 1,
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.

medium

The execution_count for this cell is 1, while others are null. According to the repository style guide (line 43), this indicates that the notebook formatting script might not have been run. Please run the formatting script to clear execution counts.

Additionally, the style guide (line 48) requires the license to be in a collapsed "Form" mode cell. This can be achieved by adding "cellView": "form" to the cell's metadata, as seen in other notebooks like Authentication.ipynb.

References
  1. If the execution_count has changed to something else than null, it usually indicates that the formatting script has not been run. A warning should be raised, but only once per notebook. (link)
  2. Include the collapsed license at the top (uses the Colab "Form" mode to hide the cells). (link)

Comment thread quickstarts/Handling_Errors_and_Retries.ipynb Outdated
"metadata": {},
"outputs": [],
"source": [
"!pip install -U google-genai"
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.

medium

The style guide requires using %pip instead of !pip (line 55). It also specifies the exact command to use for installing the SDK, which includes the -q flag and a version specifier (line 25).

%pip install -U -q 'google-genai>=1.0.0'
References
  1. use %pip instead of !pip (link)
  2. %pip install -U -q 'google-genai>=1.0.0' is the right way to install the SDK. The version indicated should reflect the minimum version needed to use the features used in the notebook (1.0.0 by default or in doubt). (link)

Comment on lines +92 to +93
"from google import genai\n",
"from google.genai import types\n",
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.

medium

The style guide (line 56) suggests placing imports right before they are used to improve readability. genai and types are first used in a later cell (line 144). To adhere to the guide, please move these two import statements to the beginning of that cell.

References
  1. Put the imports when they are first used. Try to avoid having a big "import" cell at the beginning. (link)

Comment on lines +144 to +162
"client = genai.Client(\n",
" api_key=GEMINI_API_KEY,\n",
" http_options=types.HttpOptions(\n",
" retry_options=types.HttpRetryOptions(\n",
" attempts=5,\n",
" initial_delay=2.0,\n",
" max_delay=60.0,\n",
" http_status_codes=[429, 500, 502, 503, 504]\n",
" )\n",
" )\n",
")\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents='Explain the importance of exponential backoff when calling APIs in one short paragraph.'\n",
")\n",
"\n",
"print(response.text)"
]
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.

medium

The style guide (line 59) recommends breaking code cells between "building" and "running" parts. This cell initializes the client (building) and then calls generate_content (running). For better clarity, please split this into two separate cells: one for client initialization, and another for generating content and printing the response.

References
  1. Break code cells between "building" and "running". (link)

@@ -569,7 +569,7 @@
" display(Markdown(part.text))\n",
Copy link
Copy Markdown
Collaborator

@Giom-V Giom-V Mar 18, 2026

Choose a reason for hiding this comment

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

This should be moved after "Configure model parameters" as this is not a Gemini 3 specific feature


Reply via ReviewNB

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.

@kkorpal I think you missed this 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.

@kkorpal And this one

Comment thread quickstarts/Handling_Errors_and_Retries.ipynb
@@ -0,0 +1,275 @@
{
Copy link
Copy Markdown
Collaborator

@Giom-V Giom-V Mar 18, 2026

Choose a reason for hiding this comment

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

Line #1.    %pip install -U -q 'google-genai>=1.0.0'

I don't think retry option existed in 1.0.0. Can you add the version the parameter was added instead?


Reply via ReviewNB

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.

I checked the SDK history and it was added in 1.21.1. Can you use that as the minimum version and add a comment to say it's when retry was introduced?

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.

@kkorpal you missed this request

Comment thread quickstarts/Handling_Errors_and_Retries.ipynb
Comment thread quickstarts/Handling_Errors_and_Retries.ipynb
@Giom-V
Copy link
Copy Markdown
Collaborator

Giom-V commented Mar 18, 2026

2 minor comments:

  • the notebook should end with a "what's next" section
  • add a link in the main readme too

@Giom-V
Copy link
Copy Markdown
Collaborator

Giom-V commented May 6, 2026

It's still missing a link in the main README (the one at the root of the repo)

Copy link
Copy Markdown
Collaborator Author

@kkorpal kkorpal left a comment

Choose a reason for hiding this comment

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

he missing link has been added to the root README.md

@Giom-V
Copy link
Copy Markdown
Collaborator

Giom-V commented May 13, 2026

/gemini review

Copy link
Copy Markdown
Contributor

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

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 introduces a new "Handling Errors and Retries" guide to the Gemini Cookbook, featuring a dedicated notebook and updates to existing documentation. The new notebook demonstrates both the built-in HttpRetryOptions in the google-genai SDK and custom exponential backoff implementations for handling transient API errors. Review feedback highlights the need to reset notebook execution counts per the style guide, remove redundant comments used for alignment, and delete a duplicate import to maintain code cleanliness.

Comment thread quickstarts/Handling_Errors_and_Retries.ipynb
Comment thread quickstarts/Handling_Errors_and_Retries.ipynb
},
"outputs": [],
"source": [
"from google import genai\n",
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.

medium

The google.genai module has already been imported in a previous cell. This re-import is redundant and can be removed to improve code clarity.

@Giom-V Giom-V force-pushed the feature/retry-logic-backoff branch from 23ac1e5 to a1ee8de Compare May 13, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:quickstarts Issues/PR referencing quickstarts 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