
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 2.0
/ 25
Code execution in a chat session
/ 25
Code execution in a streaming session
/ 25
This lab introduces the code execution capabilities of the Gemini 2.0 Flash model, a new multimodal generative AI model from Google DeepMind. Gemini 2.0 Flash offers improvements in speed, quality, and advanced reasoning capabilities including enhanced understanding, coding, and instruction following.
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 Gemini 2.0 Flash model.
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 and the Import libraries sections of the notebook.
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 Gemini 2.0 Flash model. You can learn about all Gemini models on Vertex AI by visiting the documentation.
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 Gemini 2.0, including how to stream the output. This capability allows you to create more dynamic and interactive applications. You can now leverage these skills to build innovative solutions and streamline your workflows. Keep exploring the possibilities of Gemini 2.0 and discover how it can empower your projects!
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 February 11, 2025
Lab Last Tested February 11, 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