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