Using Opik with Gemini

Opik integrates with Gemini to provide a simple way to log traces for all Gemini LLM calls. This works for all Gemini models.

Creating an account on Comet.com

Comet provides a hosted version of the Opik platform, simply create an account and grab you API Key.

You can also run the Opik platform locally, see the installation guide for more information.

1%pip install --upgrade opik google-genai litellm
1import opik
2
3opik.configure(use_local=False)

Preparing our environment

First, we will set up our OpenAI API keys.

1import os
2import getpass
3
4if "GOOGLE_API_KEY" not in os.environ:
5 os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Gemini API key: ")

Logging traces

Now each completion will logs a separate trace to LiteLLM:

1from google import genai
2from opik import track
3from opik.integrations.genai import track_genai
4
5os.environ["OPIK_PROJECT_NAME"] = "gemini-integration-demo"
6
7client = genai.Client()
8gemini_client = track_genai(client)
9
10prompt = """
11Write a short two sentence story about Opik.
12"""
13
14response = gemini_client.models.generate_content(
15 model="gemini-2.0-flash-001", contents=prompt
16)
17print(response.text)

The prompt and response messages are automatically logged to Opik and can be viewed in the UI.

Gemini Cookbook

Using it with the track decorator

If you have multiple steps in your LLM pipeline, you can use the track decorator to log the traces for each step. If Gemini is called within one of these steps, the LLM call with be associated with that corresponding step:

1@track
2def generate_story(prompt):
3 response = gemini_client.models.generate_content(
4 model="gemini-2.0-flash-001", contents=prompt
5 )
6 return response.text
7
8
9@track
10def generate_topic():
11 prompt = "Generate a topic for a story about Opik."
12 response = gemini_client.models.generate_content(
13 model="gemini-2.0-flash-001", contents=prompt
14 )
15 return response.text
16
17
18@track
19def generate_opik_story():
20 topic = generate_topic()
21 story = generate_story(topic)
22 return story
23
24
25generate_opik_story()

The trace can now be viewed in the UI:

Gemini Cookbook