Code Execution with Gemini | Intro to Tools

Google for Developers · Beginner ·🧠 Large Language Models ·8mo ago

Key Takeaways

Executes Python code with Gemini using the Code Execution tool to solve math problems and improve accuracy

Full Transcript

Large language models have come a long way when it comes to solving math problems, but fundamentally they still work by predicting the most probable next word in a sequence based on patterns learned during training. In other words, they aren't calcs and they aren't the most efficient or accurate at numerical computation. But by connecting large language models to tools, we can extend their abilities. So, let's check out the code execution tool, which makes Gemini more effective at problems that involve math, data analysis, computation, and code-based reasoning. Okay, I'm going to jump into Google AI Studio, and I'll make sure I have Gemini Flash latest selected as my model, and we'll type calculate the sum of the first 60 prime numbers. And I did check this beforehand. The answer is 7,699, but let's see what Gemini says here. Oh boy. All right, we got 8,368. So, I've run this query a bunch of times. Um, sometimes it works, sometimes it doesn't. Um, the model has a whole lot of hard work that it's put into here to try and solve the problem. But again, to come to the answer, it's really just using this next token mechanism um predicting the most likely next token. And that's not the best way to solve a math problem like this. So, we can use the code execution tool to give Gemini a little bit of help here and improve the ability to come up with a correct answer. So, what I'm going to do is I'm going to copy this prompt and let's restart. I'll type it in again here, but this time I'll make sure I have code execution selected in the tool toggle over here and we'll run the prompt. And this time, you can see we actually did get the correct answer, 7,699. But instead of that kind of like long string of of text that the model had produced earlier trying to come up with an answer, it's returned Python code. So while LLMs maybe aren't the best at doing actual mathematical computation, we know they're really really good at language and they're really good at generating code. So we can generate the code to solve the problem, which in this case is a function that checks if a number is a prime number or not and then adds up um the first 60 prime numbers. And this Python code is executed um on the API back end and it's returned and the result is returned to you right here which is 7,699. So not only does this make the model better at doing math, but it also gives us this nice chunk of code here where we can always refer to and see, okay, where did this answer actually come from? Okay, so let's check out a more practical example of how you might use code execution um in the real world. So, here's a little app that I made with uh Vibe Coding in AI Studio and uh it's my CSV Genius app. So, we're going to upload a file here and we'll say what fruit did I sell the most of parenthesis quantity. Okay, so let's see what this is. So, while the model's thinking, here is the uh CSV file that I uploaded. It is data from my tropical fruit stand that I wish I indeed owned. Um so there's a transaction ID, the date, the product sold, um quantity, price, etc. So the idea here is can we ask questions about this data um things that you would sort of normally as a data scientist maybe try and solve in pandas. Um but this makes it a little bit easier if you don't know how to use pandas and you don't know how to um do this kind of data analysis. We'll let the model do it for us. So, we'll jump back into the app here, and you can see it says that I sold the most of mangoes, and that was 63 units. So, that's all fine and great. Uh, but now I'm wondering, well, how did it actually come up with that answer? Um, so I have this little generated code toggle right here that I've added in. And we can see the model's loaded in the data. And then once it's done that, here is the little code snippet that Gemini wrote that was then executed. So, it loaded in my data as a data frame. It filtered for fresh fruit and then um eventually prints out the fruit that I sold the most of. So now we know where this answer came from. It was this code that was generated by Gemini that was executed on the back end and that answer was returned to um returned to the user and that's where Mango came from with 63 units. If we toggle over to the code section, you can see here in the code uh during the AI chats create method um we have Gemini 2.5 Flash selected as our model. And then we also have the code execution tool uh being passed here. And so that is how the model is able to call out to that tool to run the Python code that it generates. And that is all for code execution. But if you build something cool with this tool, definitely let me know.

Original Description

Tools extend the abilities of large language models by connecting them to the outside world. Learn how to use the Code Execution tool with Gemini, which enables the model to generate and run Python code, for problems that involve math, data analysis, and code-based reasoning. This is Part 2 of our series on using tools with the Gemini API. Subscribe to Google for Developers → https://goo.gle/developers Products Mentioned: Gemini, Google AI Studio, Gemini API
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Google for Developers · Google for Developers · 0 of 60

← Previous Next →
1 Developer Journey - Sunnyvale DSC Summit ‘19
Developer Journey - Sunnyvale DSC Summit ‘19
Google for Developers
2 How Google is working with students - Sunnyvale DSC Summit ‘19
How Google is working with students - Sunnyvale DSC Summit ‘19
Google for Developers
3 Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Starting your career in the Cloud - Sunnyvale DSC Summit ‘19
Google for Developers
4 The Solution Challenge  - Sunnyvale DSC Summit ‘19
The Solution Challenge - Sunnyvale DSC Summit ‘19
Google for Developers
5 Firebase - Sunnyvale DSC Summit ‘19
Firebase - Sunnyvale DSC Summit ‘19
Google for Developers
6 Cloud Hero - Sunnyvale DSC Summit ‘19
Cloud Hero - Sunnyvale DSC Summit ‘19
Google for Developers
7 Panel discussion  - Sunnyvale DSC Summit ‘19
Panel discussion - Sunnyvale DSC Summit ‘19
Google for Developers
8 The art of negotiation - Sunnyvale DSC Summit ‘19
The art of negotiation - Sunnyvale DSC Summit ‘19
Google for Developers
9 Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Courage to care, solve and share - Sunnyvale DSC Summit ‘19
Google for Developers
10 Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Version 9 of Angular, Glass Enterprise Edition 2, path to DX deprecation, & more!
Google for Developers
11 [DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
[DEPRECATING] Introducing a new series (Assistant for Developers Pro Tips)
Google for Developers
12 Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Detecting memory bugs with HWASan, Bazel 2.1, Next ‘20 session guide, & more!
Google for Developers
13 Why Podcast.app chose a .app domain name
Why Podcast.app chose a .app domain name
Google for Developers
14 Machine Learning Bootcamp Jakarta 2019
Machine Learning Bootcamp Jakarta 2019
Google for Developers
15 Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Android Studio 3.6, Android 11 Developer Preview, Kubeflow 1.0, & more!
Google for Developers
16 [DEPRECATING]  Importance of community (Assistant on Air)
[DEPRECATING] Importance of community (Assistant on Air)
Google for Developers
17 Why the Flutter team switched from .io to a .dev domain name
Why the Flutter team switched from .io to a .dev domain name
Google for Developers
18 3 website-building tips from .dev creators
3 website-building tips from .dev creators
Google for Developers
19 Why NimbleDroid chose a .app domain name
Why NimbleDroid chose a .app domain name
Google for Developers
20 Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Android Platform Codelab, Bazel 2.2, Maps Android Utility Library v1.0, & more!
Google for Developers
21 Google for Games Developer Summit: A free, digital experience for game developers
Google for Games Developer Summit: A free, digital experience for game developers
Google for Developers
22 Inspecting Home Graph (Assistant for Developers Pro Tips)
Inspecting Home Graph (Assistant for Developers Pro Tips)
Google for Developers
23 Google for Games Developer Summit Keynote
Google for Games Developer Summit Keynote
Google for Developers
24 Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Stadia Games & Entertainment presents: Keys to a great game pitch (Google Games Dev Summit)
Google for Developers
25 Empowering game developers with Stadia R&D (Google Games Dev Summit)
Empowering game developers with Stadia R&D (Google Games Dev Summit)
Google for Developers
26 Supercharging discoverability with Stadia (Google Games Dev Summit)
Supercharging discoverability with Stadia (Google Games Dev Summit)
Google for Developers
27 Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Stadia Games & Entertainment presents: Creating for content creators (Google Games Dev Summit)
Google for Developers
28 Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Bringing Destiny to Stadia: A postmortem (Google Games Dev Summit)
Google for Developers
29 Live Captioning in Google Slides
Live Captioning in Google Slides
Google for Developers
30 [DEPRECATING]  User engagement for the Google Assistant
[DEPRECATING] User engagement for the Google Assistant
Google for Developers
31 TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
TensorFlow Dev Summit ‘20, Google for Games Dev Summit, Cloud AI Platform Pipelines, & much more!
Google for Developers
32 Top 5 from the TensorFlow Dev Summit 2020
Top 5 from the TensorFlow Dev Summit 2020
Google for Developers
33 Developer Student Clubs 2019 Turkey Leads Summit
Developer Student Clubs 2019 Turkey Leads Summit
Google for Developers
34 Building simpler payment experiences | Google Pay Plugin for Magento 2
Building simpler payment experiences | Google Pay Plugin for Magento 2
Google for Developers
35 Become A Developer Student Club Lead
Become A Developer Student Club Lead
Google for Developers
36 Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Firebase Kotlin Extensions, ARM apps on the Android Emulator, Angular v9.1, & more!
Google for Developers
37 Test suite for Smart Home (Assistant for Developers Pro Tips)
Test suite for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
38 Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google Play updates, Bazel 3.0, Business Console for Google Pay, & more!
Google for Developers
39 How to use error logs (Assistant for Developers Pro Tips)
How to use error logs (Assistant for Developers Pro Tips)
Google for Developers
40 Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Contact Center AI, Android Studio 4.1 Canary 5, TensorFlow QAT API, & more!
Google for Developers
41 WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
WebView DevTools, Kotlin meets gRPC, Flutter CodePen support, & more! (Episode 200)
Google for Developers
42 Offline handling for Smart Home (Assistant for Developers Pro Tips)
Offline handling for Smart Home (Assistant for Developers Pro Tips)
Google for Developers
43 Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Android 11 Dev Preview 3, Google Fonts for Flutter, Shielded VM, & more!
Google for Developers
44 Machine Learning Foundations: Ep #1 - What is ML?
Machine Learning Foundations: Ep #1 - What is ML?
Google for Developers
45 Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Flutter web support updates, BigQuery materialized views, Cloud Spanner emulator, & more!
Google for Developers
46 Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Computer vision by building a neural network with TensorFlow | Machine Learning Foundations
Google for Developers
47 Machine Learning Foundations: Ep #3 - Convolutions and pooling
Machine Learning Foundations: Ep #3 - Convolutions and pooling
Google for Developers
48 Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Android 11 Beta plans, Flutter 1.17, Dart 2.8, & much more!
Google for Developers
49 Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Machine Learning Foundations: Ep #4 - Coding with Convolutional Neural Networks
Google for Developers
50 Google Developers ML Summit
Google Developers ML Summit
Google for Developers
51 Real-world image classification using convolutional neural networks | Machine Learning Foundations
Real-world image classification using convolutional neural networks | Machine Learning Foundations
Google for Developers
52 Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Adobe XD support for Flutter, Architecture Framework, temporary closures with Places API, & more!
Google for Developers
53 Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Machine Learning Foundations: Ep #6 - Convolutional cats and dogs
Google for Developers
54 Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
Google for Developers
55 Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Announcing Firebase Live, Flutter Day, Java 11 on Google Cloud Functions, & more!
Google for Developers
56 Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Machine Learning Foundations: Ep #8 - Tokenization for Natural Language Processing
Google for Developers
57 Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Android 11 Beta, Google Play Asset Delivery, Firebase Crashlytics SDK, & much more!
Google for Developers
58 Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Natural Language Processing: Using sequencing APIs in TensorFlow | Machine Learning Foundations
Google for Developers
59 Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Build a sarcasm classifier using NLP and TensorFlow | Machine Learning Foundations
Google for Developers
60 AR Realism with the ARCore Depth API
AR Realism with the ARCore Depth API
Google for Developers

Related Reads

📰
I compared the real cost of running LLMs on AWS - here's when each option makes sense
Learn when to use each AWS option for running LLMs in production and understand their cost implications
Dev.to · Jerzy Kopaczewski
📰
Building a Character-Level Bigram Language Model from Scratch with PyTorch
Learn to build a basic character-level bigram language model from scratch using PyTorch, understanding the fundamentals of neural language modeling
Dev.to · Mohamed Heni
📰
Running NVIDIA Nemotron 3.5 ASR Locally with parakeet.cpp (and how it beat Whisper on my laptop)
Run NVIDIA Nemotron 3.5 ASR locally for offline speech-to-text capabilities without relying on cloud services or incurring API bills
Medium · LLM
📰
When Does a Prompt Become an Undocumented Program?
Learn to identify when a prompt becomes an undocumented program and why it matters for effective AI integration in analyst work
Dev.to · Yura Solovey
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →