Skip to content

Commit 77abf41

Browse files
committed
GenAI upload datetime JSON with warning
1 parent 6276543 commit 77abf41

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_upload/completion_hook.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
import datetime
1819
import json
1920
import logging
2021
import posixpath
@@ -287,4 +288,14 @@ class Base64JsonEncoder(json.JSONEncoder):
287288
def default(self, o: Any) -> Any:
288289
if isinstance(o, bytes):
289290
return b64encode(o).decode()
290-
return super().default(o)
291+
elif isinstance(o, (datetime.datetime, datetime.date)):
292+
return o.isoformat()
293+
294+
try:
295+
return super().default(o)
296+
except TypeError:
297+
_logger.warning(
298+
'failed to encode object "%s" to JSON. Falling back to str()',
299+
o,
300+
)
301+
return str(o)

0 commit comments

Comments
 (0)