Skip to content

Commit 27d8eac

Browse files
committed
GenAI upload datetime JSON with warning
1 parent 7ec6c23 commit 27d8eac

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
@@ -271,4 +272,14 @@ class Base64JsonEncoder(json.JSONEncoder):
271272
def default(self, o: Any) -> Any:
272273
if isinstance(o, bytes):
273274
return b64encode(o).decode()
274-
return super().default(o)
275+
elif isinstance(o, (datetime.datetime, datetime.date)):
276+
return o.isoformat()
277+
278+
try:
279+
return super().default(o)
280+
except TypeError:
281+
_logger.warning(
282+
'failed to encode object "%s" to JSON. Falling back to str()',
283+
o,
284+
)
285+
return str(o)

0 commit comments

Comments
 (0)