feat: Add "Handling Errors and Retries" quickstart notebook (Resolves #469)#1160
feat: Add "Handling Errors and Retries" quickstart notebook (Resolves #469)#1160kkorpal wants to merge 9 commits into
Conversation
|
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 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
Changelog
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 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 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
|
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "!pip install -U google-genai" |
There was a problem hiding this comment.
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'
| "from google import genai\n", | ||
| "from google.genai import types\n", |
There was a problem hiding this comment.
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
- Put the imports when they are first used. Try to avoid having a big "import" cell at the beginning. (link)
| "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)" | ||
| ] |
There was a problem hiding this comment.
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
- Break code cells between "building" and "running". (link)
… error handling examples
… error handling examples
| @@ -569,7 +569,7 @@ | |||
| " display(Markdown(part.text))\n", | |||
| @@ -0,0 +1,275 @@ | |||
| { | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
|
2 minor comments:
|
…n requirements, and updated README links
|
It's still missing a link in the main README (the one at the root of the repo) |
kkorpal
left a comment
There was a problem hiding this comment.
he missing link has been added to the root README.md
|
/gemini review |
There was a problem hiding this comment.
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.
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from google import genai\n", |
23ac1e5 to
a1ee8de
Compare
…esolves #469)