LLMs like Google’s Gemini are good at writing code, but can’t execute code. Fortunately, Gemini understands this and allows you to supplement its functionality with external “tools”.

In this guide we’ll hook up the Riza Code Interpreter API as a tool for Gemini to use when it wants to execute code. Using Riza as the code execution environment keeps your local machine safe in case the code written by Gemini does something unexpected.

We’ll run Python, but Riza can execute JavaScript, PHP, and Ruby too.

Example code

Get the full code for this example in our GitHub.

Getting started

To generate Python with Gemini, you’ll need an API key from the Google.

To execute Python using Riza’s Code Interpreter API, you’ll need an API key from the Riza Dashboard.

Make these API keys available in your shell environment:

export GEMINI_API_KEY="your_gemini_api_key"
export RIZA_API_KEY="your_riza_api_key"

In this guide we’ll use the Gemini and Riza Python API client libraries.

Python environment setup

Create a virtualenv and activate it:

python3 -m venv venv
source venv/bin/activate

Install the google-generativeai and rizaio packages with pip:

pip install google-generativeai rizaio

Scenario

We’ll create a message for Gemini that requires code execution to answer. Here, we ask Gemini to base32 encode the message “purple monkey dishwasher.”

response = chat.send_message('Please base32 encode this message: purple monkey dishwasher')

Sending this message to Gemini without offering any additional tools results in unexpected hallucinated output:

Response
ONXSA5DTEBUXGIDQOJQW4Y3PNVXXE33VNFSA====

The correct result is OB2XE4DMMUQG233ONNSXSIDENFZWQ53BONUGK4Q=.

For a correct result, we can offer Gemini a tool to execute Python.

Step 1: Import required libraries

First we import and initialize required libraries from Google and Riza.

import os
import rizaio
import google.generativeai as genai

genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
riza = rizaio.Riza()

Step 2: Define a code execution tool

Before sending the message to Gemini we’ll describe the Riza Code Interpreter as a tool using a tool definition that Claude understands.

In this example, we will use Riza’s Execute Code API to run the code. So first, we define an execute_python helper function in our script. This function uses Riza to execute arbitrary Python:

def execute_python(code:str):
    """ Executes a Python script and returns whatever was printed to stdout.

    The Python runtime does not have filesystem access, but does include the entire standard library. Read input from stdin and write output to stdout.
    """
    resp = riza_client.command.exec(
        language="python",
        code=code
    )
    return resp.stdout

We’ll use this function in the next step.

Step 3: Call Gemini with Riza as a tool

Now, we can make execute_python() function available to Gemini as a tool, and call Gemini with our query:

def main():
    GEMINI_MODEL = "gemini-2.0-flash"

    model = genai.GenerativeModel(
        GEMINI_MODEL,
        tools=[execute_python]
    )
    chat = model.start_chat(enable_automatic_function_calling=True)

    response = chat.send_message('Please base32 encode this message (use a tool if needed): purple monkey dishwasher')
    print(response.text)

Here, we enable the enable_automatic_function_calling setting to allow Gemini to call any function without us having to explicitly handle the tool use. If you’d like to explicitly handle the tool use, you can disable this setting.

Finally, let’s print out the full chat history so we can see the function call:

    for content in chat.history:
        print(content.role, "->", [type(part).to_dict(part) for part in content.parts])
        print('-'*80)

See the full example in GitHub.