From 2313f5f90a4f56d01406105bfe0e4adbf3d7bc8f Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Wed, 6 Mar 2024 10:21:25 -0800 Subject: [PATCH] (feat) handle litellm circular ref error --- litellm/proxy/utils.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index 527b66f8c0c..ddce2b221f4 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -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),