Log media & attachments

Opik supports multimodal traces allowing you to track not just the text input and output of your LLM, but also images, videos and audio and any other media.

Logging Attachments

In the Python SDK, you can use the Attachment type to add files to your traces. Attachements can be images, videos, audio files or any other file that you might want to log to Opik.

Each attachment is made up of the following fields:

  • data: The path to the file or the base64 encoded string of the file
  • content_type: The content type of the file formatted as a MIME type

These attachements can then be logged to your traces and spans using The opik_context.update_current_span and opik_context.update_current_trace methods:

1from opik import opik_context, track, Attachment
2
3@track
4def my_llm_agent(input):
5 # LLM chain code
6 # ...
7
8 # Update the trace
9 opik_context.update_current_trace(
10 attachments=[
11 Attachment(
12 data="<path to the image>",
13 content_type="image/png",
14 )
15 ]
16 )
17
18 return "World!"
19
20print(my_llm_agent("Hello!"))

The attachements will be uploaded to the Opik platform and can be both previewed and dowloaded from the UI.

In order to preview the attachements in the UI, you will need to supply a supported content type for the attachment. We support the following content types:

  • Image: image/jpeg, image/png, image/gif and image/svg+xml
  • Video: video/mp4 and video/webm
  • Audio: audio/wav, audio/vorbis and audio/x-wav
  • Text: text/plain and text/markdown
  • PDF: application/pdf
  • Other: application/json and application/octet-stream

Previewing base64 encoded images and image URLs

Opik automatically detects base64 encoded images and URLs logged to the platform, once an image is detected we will hide the string to make the content more readable and display the image in the UI. This is supported in the tracing view, datasets view and experiment view.

For example if you are using the OpenAI SDK, if you pass an image to the model as a URL, Opik will automatically detect it and display the image in the UI:

1from opik.integrations.openai import track_openai
2from openai import OpenAI
3
4# Make sure to wrap the OpenAI client to enable Opik tracing
5client = track_openai(OpenAI())
6
7response = client.chat.completions.create(
8 model="gpt-4o-mini",
9 messages=[
10 {
11 "role": "user",
12 "content": [
13 {"type": "text", "text": "What's in this image?"},
14 {
15 "type": "image_url",
16 "image_url": {
17 "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
18 },
19 },
20 ],
21 }
22 ],
23 max_tokens=300,
24)
25
26print(response.choices[0])

Downloading attachments

You can download attachments from the UI by hovering over the attachments and clicking on the download icon. SDKs methods are not yet available but it’s coming soon !

Let’s us know on Github if you would like to us to support additional image formats.