Gemini 2.0 Deep Dive: Code Execution

3월 06, 2025
Jason Stephen Product Manager
Luciano Martins Developer Advocate Google AI

Code execution gives Gemini models access to a Python sandbox, allowing the models to run code and learn from the results. With code execution enabled, Gemini models can perform calculations, analyze complex data sets, and create visualizations on the fly - ultimately providing better answers to user queries. It is now generally available in both Google AI Studio and the Gemini API via the Gemini 2.0 models.


Code execution as a tool

You can enable code execution via toggle in Google AI Studio’s “Tools” panel, or from the Gemini API as a tools variable (shown below).

from google import genai
from google.genai import types

client = genai.Client(api_key="GEMINI_API_KEY")

response = client.models.generate_content(
  model='gemini-2.0-flash',
  contents="""
     What is the sum of the first 50 prime numbers?
     Generate and run code for the calculation.
  """,
  config=types.GenerateContentConfig(
    tools=[types.Tool(
      code_execution=types.ToolCodeExecution
    )]
  )
)

When code execution is added as a tool, the model can access the code execution sandbox to run code for up to 30 seconds at a time, and up to 5 times without re-prompting. The code execution environment includes libraries like Numpy, Pandas, and Matplotlib (for graph rendering). The full list of available libraries can be found in our API docs and we are looking to expand the set of supported libraries soon.


File IO and Graph Output

With Gemini 2.0, we’ve updated our code execution tool to allow file input into the code execution sandbox, as well as graphs & chart output using Matplotlib. These updates lead to a broader set of use cases for code execution. With these updates you can now:

  • Perform logical analysis to understand complex user-uploaded files.

  • Visualize data with charts and graphs supported by Matplotlib.

  • Debug local code files.

  • [Experimental] Unlock code execution's real-time capabilities with the Multimodal Live API.

  • [Experimental] Combine code execution with tools like Grounding with Google Search.

  • [Experimental] Leverage code execution from Gemini 2.0 Thinking Mode.

  • And more...

Let's look at two practical examples of code execution in action:


Real-time data analysis and visualization with Gemini models

By combining the Multimodal Live API and code execution, this demo demonstrates a live conversation with the Gemini model, utilizing both voice input and code execution. We ask Gemini models to provide a list of Tom Cruise movies, rank them by runtime, and then create a bar chart visualizing this data using Matplotlib. Gemini models generate the Python code to perform these tasks and update the chart based on our further requests (changing bar colors).

(Right-click and open video in new window to hear audio)

Combining Thinking model and code execution to solve complex problems

This demo uses the Gemini 2.0 Flash Thinking Experimental model and code execution to solve a classic optimization challenge. We ask Gemini to find the shortest route for a salesperson visiting five Spanish cities and returning to their starting point. Gemini generates Python code, debugs it iteratively (correcting an initial library error), calculates distances, and ultimately visualizes the optimal route on a Matplotlib graph.

(Right-click and open video in new window to hear audio)

Get started with Gemini 2.0 Code Execution

Want to try yourself? Check out the GitHub to get hands-on with code execution. For further examples and demos please visit the Gemini API docs, where you can explore scenarios of code execution being used and more.


We welcome you to join the Gemini API Developer forum to share your use cases and feedback on how we can improve code execution for you. In the short term, we are exploring expanded library support, supporting other input modalities like PDFs, and supporting multi-tool use. Happy building!