Skip to content

Commit ae4da64

Browse files
committed
fix: encode bytes as base64 when dumping to json string.
1 parent 166c081 commit ae4da64

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from .dict_util import flatten_dict
6262
from .flags import is_content_recording_enabled
6363
from .message import (
64+
Base64JsonEncoder,
6465
to_input_messages,
6566
to_output_messages,
6667
to_system_instructions,
@@ -279,7 +280,7 @@ def _create_completion_details_attributes(
279280
]
280281

281282
if as_str:
282-
return {k: json.dumps(v) for k, v in attributes.items()}
283+
return {k: json.dumps(v, cls=Base64JsonEncoder) for k, v in attributes.items()}
283284

284285
return attributes
285286

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
from __future__ import annotations
1616

17+
from base64 import b64encode
18+
import json
1719
import logging
1820
from enum import Enum
21+
from typing import Any
1922

2023
from google.genai import types as genai_types
2124
from opentelemetry.util.genai.types import (
@@ -38,6 +41,13 @@ class Role(str, Enum):
3841
TOOL = "tool"
3942

4043

44+
class Base64JsonEncoder(json.JSONEncoder):
45+
def default(self, o: Any) -> Any:
46+
if isinstance(o, bytes):
47+
return b64encode(o).decode()
48+
return super().default(o)
49+
50+
4151
_logger = logging.getLogger(__name__)
4252

4353

0 commit comments

Comments
 (0)