Skip to content

Conversation

@Rahul-Lashkari
Copy link
Contributor

  • Added JS equivalent for quickstarts/Error_handling.ipynb notebook
  • updated quickstarts-js/README.md

@gemini-code-assist
Copy link
Contributor

Summary of Changes

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

  • New JavaScript Notebook: A new notebook, quickstarts-js/Error_handling.js, has been added to demonstrate various error handling strategies for the Gemini API.
  • Error Handling Strategies: The new notebook covers automatic retries for transient errors, manual adjustment of API call timeouts, and implementing custom backoff and retry logic with exponential backoff.
  • README Update: The quickstarts-js/README.md file has been updated to include a new entry for the 'Error Handling' notebook, making it discoverable for users.
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 the status:awaiting review PR awaiting review from a maintainer label Oct 30, 2025
Copy link
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 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.

Comment on lines +53 to +71
### 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`
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This documentation section about automatic retries appears to be copied from a Python notebook and is incorrect for the JavaScript SDK.

  • The request_options argument for retry configuration is a feature of the Python SDK.
  • The link on line 64 points to the Python google-api-core library.
  • 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.

Comment on lines +135 to +139
const response = await ai.models.generateContent({
model: MODEL_ID,
contents: prompt,
requestOptions: requestOptions,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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);

Comment on lines +47 to +49
module = await import("https://esm.sh/@google/genai@1.4.0");
GoogleGenAI = module.GoogleGenAI;
ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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 });

Comment on lines +74 to +75
MODEL_ID = "gemini-2.5-flash";
prompt = "Write a story about a magic backpack.";
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Variables MODEL_ID and prompt are used without declaration. Please declare them with const.

const MODEL_ID = "gemini-2.5-flash";
const prompt = "Write a story about a magic backpack.";

MODEL_ID = "gemini-2.5-flash";
prompt = "Write a story about a magic backpack.";

response = await ai.models.generateContent({
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The response variable is used without declaration. Please declare it with const.

const response = await ai.models.generateContent({

*/

// [CODE STARTS]
prompt = "Write a story about a magic backpack.";
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The prompt variable is used without declaration. Please declare it with const.

const prompt = "Write a story about a magic backpack.";

Comment on lines +174 to +177
### 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.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The documentation for manual retry implementation links to Python libraries (retry, backoff, tenacity). This is a JavaScript file, and these links are irrelevant and confusing for JS developers. Please remove the links or replace them with links to equivalent JavaScript libraries.

Comment on lines +206 to +207
prompt = "Write a one-liner advertisement for magic backpack.";
response = await generateWithRetry(prompt);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Variables prompt and response are used without declaration. Please declare them with const.

const prompt = "Write a one-liner advertisement for magic backpack.";
const response = await generateWithRetry(prompt);

// [CODE STARTS]
let callCounter = 0;

const delaySim = (ms) => new Promise(resolve => setTimeout(resolve, ms));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The function delaySim is identical to the delay function defined on line 180. To avoid code duplication, please remove delaySim and use delay on line 267.

Comment on lines +277 to +278
prompt = "Write a one-liner advertisement for magic backpack.";
response = await generateContentFirstFail(prompt);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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);

@andycandy
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:awaiting review PR awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants