(feat) handle litellm circular ref error

This commit is contained in:
ishaan-jaff 2024-03-06 10:21:25 -08:00
parent 656e300b83
commit 2313f5f90a

View file

@ -507,6 +507,18 @@ class PrismaClient:
return hashed_token
def handle_circular_references(obj: Any):
try:
# Try to serialize the object using default serialization
return json.dumps(obj)
except Exception as e:
# If a TypeError is raised, return an error message JSON
error_message: dict = {
"error": "Error converting to JSON",
"error_message": str(e),
}
return json.dumps(error_message)
def jsonify_object(self, data: dict) -> dict:
db_data = copy.deepcopy(data)
@ -1641,10 +1653,27 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time):
if api_key is not None and isinstance(api_key, str) and api_key.startswith("sk-"):
# hash the api_key
api_key = hash_token(api_key)
if "headers" in metadata and "authorization" in metadata["headers"]:
metadata["headers"].pop(
"authorization"
) # do not store the original `sk-..` api key in the db
# clean up litellm metadata
if isinstance(metadata, dict):
clean_metadata = {}
verbose_proxy_logger.debug(
f"getting payload for SpendLogs, available keys in metadata: "
+ str(list(metadata.keys()))
)
for key in metadata:
if key in [
"headers",
"endpoint",
"model_group",
"deployment",
"model_info",
"caching_groups",
]:
continue
else:
clean_metadata[key] = metadata[key]
if litellm.cache is not None:
cache_key = litellm.cache.get_cache_key(**kwargs)
else:
@ -1668,7 +1697,7 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time):
"team_id": kwargs.get("litellm_params", {})
.get("metadata", {})
.get("user_api_key_team_id", ""),
"metadata": metadata,
"metadata": clean_metadata,
"cache_key": cache_key,
"spend": kwargs.get("response_cost", 0),
"total_tokens": usage.get("total_tokens", 0),