Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Set up the notebook
/ 25
Working with code execution in Gemini
/ 25
Code execution in a chat session
/ 25
Code execution in a streaming session
/ 25
In this lab, you will explore the code execution capabilities of
A key feature of this model is code execution, which is the ability to generate and execute Python code directly within the API. If you want the API to generate and run Python code and return the results, you can use code execution as demonstrated in this lab.
This code execution capability enables the model to generate code, execute and observe the results, correct the code if needed, and learn iteratively from the results until it produces a final output. This is particularly useful for applications that involve code-based reasoning such as solving mathematical equations or processing text.
Gemini is a family of powerful generative AI models developed by Google DeepMind, capable of understanding and generating various forms of content, including text, code, images, audio, and video.
The Gemini API in Vertex AI provides a unified interface for interacting with Gemini models. This allows developers to easily integrate these powerful AI capabilities into their applications. For the most up-to-date details and specific features of the latest versions, please refer to the official Gemini documentation.
Before starting this lab, you should be familiar with:
In this lab, you will learn how to generate and execute code using the Gemini API in Vertex AI and the Google Gen AI SDK for Python with the
You will complete the following tasks:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
In the Google Cloud console, on the Navigation menu (), click Vertex AI > Workbench.
Find the
The JupyterLab interface for your Workbench instance opens in a new browser tab.
Open the
In the Select Kernel dialog, choose Python 3 from the list of available kernels.
Run through the Getting Started section of the notebook. The Project ID and Location are pre-configured for you.
Click Check my progress to verify the objective.
In this section, you will use the Gemini API to generate and execute Python code.
The following code in the notebook loads the
The following code in the notebook initializes the code execution tool by passing code_execution in a Tool definition. Later you'll register this tool with the model so it can use it to generate and run Python code.
The following code in the notebook sends a prompt to the Gemini model, asking it to generate and execute Python code to calculate the sum of the first 50 prime numbers. The code execution tool is passed in so the model can generate and run the code.
The following code in the notebook iterates through the response and displays any generated Python code by checking for part.executable_code in the response parts.
The following code in the notebook iterates through the response and displays the execution result and outcome by checking for part.code_execution_result in the response parts.
Click Check my progress to verify the objective.
This section shows how to use code execution in an interactive chat with history using the Gemini API. You can create a chat session, enabling the model to generate and run Python code. You'll start by asking the model to generate sample time series data with noise and output a sample of 10 data points.
You can then iterate through the response to display any generated code and execution results. Next, you'll ask the model to add a smoothed data series to the time series data and display the results. Finally, you'll ask the model to generate descriptive statistics for the time series data and display the results.
Click Check my progress to verify the objective.
You can also use the code execution functionality with streaming output from the Gemini API. The following code demonstrates how the Gemini API can generate and execute code while streaming the results.
Click Check my progress to verify the objective.
Congratulations! You have successfully learned how to generate and execute Python code with
Check out the following resources to learn more about Gemini:
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated October 28, 2025
Lab Last Tested October 28, 2025
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one