> ## Documentation Index
> Fetch the complete documentation index at: https://docs.riza.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini

> Connect Riza's code interpreter to Gemini

LLMs like Google's [Gemini](https://gemini.google.com/) 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](https://github.com/riza-io/examples/blob/main/platforms/gemini_code.py).

## Getting started

To generate Python with Gemini, you'll need an API key from the [Google](https://aistudio.google.com/app/apikey).

To execute Python using Riza's Code Interpreter API, you'll need an API key from the [Riza Dashboard](https://dashboard.riza.io/).

Make these API keys available in your shell environment:

```sh theme={null}
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:

```sh theme={null}
python3 -m venv venv
source venv/bin/activate
```

Install the `google-generativeai` and `rizaio` packages with `pip`:

```sh theme={null}
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."

```py theme={null}
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:

```text Response theme={null}
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.

```py theme={null}
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](https://docs.anthropic.com/en/docs/build-with-claude/tool-use/overview#specifying-tools)
that Claude understands.

In this example, we will use Riza's [Execute Code](https://docs.riza.io/api-reference/command/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:

```py theme={null}
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:

```py theme={null}
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:

```py theme={null}
    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](https://github.com/riza-io/examples/blob/main/platforms/gemini_code.py).
