-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Added js version of Error_handling notebook #1019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Added js version of Error_handling notebook #1019
Conversation
Summary of ChangesHello @Rahul-Lashkari, 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 new JavaScript notebook dedicated to robust error handling when interacting with the Gemini API. It provides practical examples and guidance on managing common API issues such as transient errors, rate limits, and timeouts, ultimately enhancing the reliability and resilience of applications built using the Gemini API. The accompanying README has also been updated to reflect this new addition. 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 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.
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 adds a JavaScript version of the Error_handling notebook. The new file provides examples for handling transient errors, rate limits, and timeouts when using the Gemini API. My review focuses on correcting documentation that was copied from the Python version without adaptation, fixing incorrect API usage, and improving general JavaScript code quality by addressing issues like undeclared variables and code duplication.
| ### Automatic retries | ||
|
|
||
| The Gemini API's client library offers built-in retry mechanisms for handling transient errors. You can enable this feature by using the `request_options` argument with API calls like `generate_content`, `generate_answer`, `embed_content`, and `generate_content_async`. | ||
|
|
||
| **Advantages:** | ||
|
|
||
| * **Simplicity:** Requires minimal code changes for significant reliability gains. | ||
| * **Robust:** Effectively addresses most transient errors without additional logic. | ||
|
|
||
| **Customize retry behavior:** | ||
|
|
||
| Use these settings in [`retry`](https://googleapis.dev/python/google-api-core/latest/retry.html) to customize retry behavior: | ||
|
|
||
| * `predicate`: (callable) Determines if an exception is retryable. Default: [`if_transient_error`](https://github.com/googleapis/python-api-core/blob/main/google/api_core/retry/retry_base.py#L75C4-L75C13) | ||
| * `initial`: (float) Initial delay in seconds before the first retry. Default: `1.0` | ||
| * `maximum`: (float) Maximum delay in seconds between retries. Default: `60.0` | ||
| * `multiplier`: (float) Factor by which the delay increases after each retry. Default: `2.0` | ||
| * `timeout`: (float) Total retry duration in seconds. Default: `120.0` | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation section about automatic retries appears to be copied from a Python notebook and is incorrect for the JavaScript SDK.
- The
request_optionsargument for retry configuration is a feature of the Python SDK. - The link on line 64 points to the Python
google-api-corelibrary. - The retry parameters listed (
predicate,initial, etc.) are for the Python library.
This is misleading for JavaScript developers. Please update this section to reflect the capabilities of the @google/genai JavaScript SDK.
| const response = await ai.models.generateContent({ | ||
| model: MODEL_ID, | ||
| contents: prompt, | ||
| requestOptions: requestOptions, | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The requestOptions object is being passed inside the generateContent request body. This is not the correct way to specify request options like timeout for the @google/genai SDK, and this option will likely be ignored. To set a timeout, you should either create a new GoogleGenAI client with the desired requestOptions or use ai.getGenerativeModel() to get a model instance with custom options.
const modelWithTimeout = ai.getGenerativeModel({ model: MODEL_ID }, requestOptions);
const response = await modelWithTimeout.generateContent(prompt);| module = await import("https://esm.sh/@google/genai@1.4.0"); | ||
| GoogleGenAI = module.GoogleGenAI; | ||
| ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables module, GoogleGenAI, and ai are being assigned without declaration (const, let, or var). This creates global variables, which is a bad practice in JavaScript. Please declare them using const since they are not reassigned.
const module = await import("https://esm.sh/@google/genai@1.4.0");
const GoogleGenAI = module.GoogleGenAI;
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });| MODEL_ID = "gemini-2.5-flash"; | ||
| prompt = "Write a story about a magic backpack."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| MODEL_ID = "gemini-2.5-flash"; | ||
| prompt = "Write a story about a magic backpack."; | ||
|
|
||
| response = await ai.models.generateContent({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| */ | ||
|
|
||
| // [CODE STARTS] | ||
| prompt = "Write a story about a magic backpack."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ### Manually implement backoff and retry with error handling | ||
|
|
||
| For finer control over retry behavior and error handling, you can use the [`retry`](https://googleapis.dev/python/google-api-core/latest/retry.html) library (or similar libraries like [`backoff`](https://pypi.org/project/backoff/) and [`tenacity`](https://tenacity.readthedocs.io/en/latest/)). This gives you precise control over retry strategies and allows you to handle specific types of errors differently. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| prompt = "Write a one-liner advertisement for magic backpack."; | ||
| response = await generateWithRetry(prompt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // [CODE STARTS] | ||
| let callCounter = 0; | ||
|
|
||
| const delaySim = (ms) => new Promise(resolve => setTimeout(resolve, ms)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| prompt = "Write a one-liner advertisement for magic backpack."; | ||
| response = await generateContentFirstFail(prompt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables prompt and response are used without declaration. Additionally, the generateContentFirstFail function relies on a global callCounter which is not reset, making the test non-idempotent. Please declare the variables and reset the counter before the call to ensure the test is reliable.
const prompt = "Write a one-liner advertisement for magic backpack.";
callCounter = 0; // Reset counter before test
const response = await generateContentFirstFail(prompt);|
Hi @Rahul-Lashkari, thank you for your contribution! However, the JavaScript file you submitted contains several documentation issues for example, it references Python packages and sources instead of JavaScript ones. Additionally, the error handling section doesn’t fully align with the original notebook. Please make these adjustments in your PR. |
quickstarts/Error_handling.ipynbnotebookquickstarts-js/README.md