Skip to content

feat(js): add Code Execution quickstart in Javascript(js)#1086

Open
ADITYAKUMARRAI2007 wants to merge 2 commits intogoogle-gemini:mainfrom
ADITYAKUMARRAI2007:fix-code-execution-retry
Open

feat(js): add Code Execution quickstart in Javascript(js)#1086
ADITYAKUMARRAI2007 wants to merge 2 commits intogoogle-gemini:mainfrom
ADITYAKUMARRAI2007:fix-code-execution-retry

Conversation

@ADITYAKUMARRAI2007
Copy link

Summary

This PR adds a missing JavaScript quickstart example for the Code Execution feature. Previously, this example was not available in the quickstarts-js directory.

Implementation Details

  • New File: Added quickstarts-js/Code_Execution.js.
  • Model: Uses gemini-2.5-flash-lite. This ensures the example runs reliably on the Free Tier without hitting the strict limits of experimental models.
  • SDK Usage: Demonstrates how to enable the codeExecution tool and correctly parse the response using the @google/genai SDK.

Verification

  • Tested locally.
  • Verified the model generates and executes Python code.
  • Output confirmed: Sum of first 50 prime numbers is 5117.

@google-cla
Copy link

google-cla bot commented Dec 29, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the status:awaiting review PR awaiting review from a maintainer label Dec 29, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ADITYAKUMARRAI2007, 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 addresses a gap in the existing documentation by providing a JavaScript quickstart for the Code Execution feature. It offers a practical example of how to integrate and use the codeExecution tool with the Gemini API, ensuring developers can easily get started with this capability.

Highlights

  • New Quickstart Example: Introduced a new JavaScript quickstart example for the Code Execution feature, which was previously missing from the quickstarts-js directory.
  • Model Usage: The example utilizes the gemini-2.5-flash-lite model, chosen for its reliability and compatibility with the Free Tier limits, avoiding experimental models.
  • SDK Implementation: Demonstrates the correct usage of the @google/genai SDK to enable the codeExecution tool and effectively parse the generated response.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

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 PR adds a new JavaScript quickstart for the Code Execution feature. The implementation is straightforward, but I have a few suggestions to improve it. The most important one is to enhance the response logging to properly demonstrate the code execution feature by showing the generated code and its output, not just the final text summary. I've also suggested a change for consistency in naming conventions and a minor fix for file formatting. Additionally, please remember to update the quickstarts-js/README.md to include this new example in the table of contents for better discoverability.

Comment on lines +41 to +43
// The response includes the model's thought process, the code it ran, and the final answer.
console.log(response.text);

Copy link
Contributor

Choose a reason for hiding this comment

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

high

Simply logging response.text is insufficient for a code execution quickstart, as it only shows the model's final summary. To properly demonstrate the feature, you should parse the response to show the executable code and its output. This gives the user a much clearer understanding of what the Code Execution tool does. The Get_Started.js file provides a good example of this.

    // The response includes the model's thought process, the code it ran, and the final answer.
    for (const part of response.candidates[0].content.parts) {
      if (part.text) {
        console.log(part.text);
      } else if (part.executableCode) {
        console.log('--- Executable Code ---');
        console.log(part.executableCode.code);
      } else if (part.codeExecutionResult) {
        console.log('--- Code Execution Result ---');
        console.log(part.codeExecutionResult.output);
      }
    }

async function run() {
console.log("--- Sending request to Gemini with Code Execution enabled ---");

const model = "gemini-2.5-flash-lite";
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency with other JavaScript quickstarts in this repository, it's better to name this constant MODEL_ID. This makes it easier for users to identify and change the model being used. Please also update its usage on line 33.

Suggested change
const model = "gemini-2.5-flash-lite";
const MODEL_ID = "gemini-2.5-flash-lite";

}
}

run(); No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It's a good practice to end files with a newline character. This can prevent issues with some tools and file concatenation.

Copy link
Author

@ADITYAKUMARRAI2007 ADITYAKUMARRAI2007 left a comment

Choose a reason for hiding this comment

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

new changes made

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.

1 participant