mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 19:54:13 +00:00
Better JSON serialization for Datadog logs
Dicts are now properly serialized to JSON so that Datadog can parse the child attributes. Also, numbers and nulls are sent as numbers and nulls instead of strings.
This commit is contained in:
parent
b8f7edfa0a
commit
66c1b54ea2
1 changed files with 15 additions and 7 deletions
|
@ -9,6 +9,20 @@ import litellm, uuid
|
|||
from litellm._logging import print_verbose, verbose_logger
|
||||
|
||||
|
||||
def make_json_serializable(payload):
|
||||
for key, value in payload.items():
|
||||
try:
|
||||
if isinstance(value, dict):
|
||||
# recursively sanitize dicts
|
||||
payload[key] = make_json_serializable(value.copy())
|
||||
if not isinstance(value, (str, int, float, bool, type(None))):
|
||||
# everything else becomes a string
|
||||
payload[key] = str(value)
|
||||
except:
|
||||
# non blocking if it can't cast to a str
|
||||
pass
|
||||
|
||||
|
||||
class DataDogLogger:
|
||||
# Class variables or attributes
|
||||
def __init__(
|
||||
|
@ -104,13 +118,7 @@ class DataDogLogger:
|
|||
"metadata": clean_metadata,
|
||||
}
|
||||
|
||||
# Ensure everything in the payload is converted to str
|
||||
for key, value in payload.items():
|
||||
try:
|
||||
payload[key] = str(value)
|
||||
except:
|
||||
# non blocking if it can't cast to a str
|
||||
pass
|
||||
make_json_serializable(payload)
|
||||
import json
|
||||
|
||||
payload = json.dumps(payload)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue