Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat log fail events on gcs

Ishaan Jaff63af2942ab31
diff --git a/litellm/integrations/gcs_bucket.py b/litellm/integrations/gcs_bucket.py
index 46f55f8f01c..6525f680a1a 100644
--- a/litellm/integrations/gcs_bucket.py
+++ b/litellm/integrations/gcs_bucket.py
@@ -1,5 +1,6 @@
import json
import os
+import uuid
from datetime import datetime
from typing import Any, Dict, List, Optional, TypedDict, Union
@@ -29,6 +30,8 @@ class GCSBucketPayload(TypedDict):
end_time: str
response_cost: Optional[float]
spend_log_metadata: str
+ exception: Optional[str]
+ log_event_type: Optional[str]
class GCSBucketLogger(CustomLogger):
@@ -79,6 +82,7 @@ class GCSBucketLogger(CustomLogger):
logging_payload: GCSBucketPayload = await self.get_gcs_payload(
kwargs, response_obj, start_time_str, end_time_str
)
+ logging_payload["log_event_type"] = "successful_api_call"
json_logged_payload = json.dumps(logging_payload)
@@ -103,7 +107,49 @@ class GCSBucketLogger(CustomLogger):
verbose_logger.error("GCS Bucket logging error: %s", str(e))
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
- pass
+ from litellm.proxy.proxy_server import premium_user
+
+ if premium_user is not True:
+ raise ValueError(
+ f"GCS Bucket logging is a premium feature. Please upgrade to use it. {CommonProxyErrors.not_premium_user.value}"
+ )
+ try:
+ verbose_logger.debug(
+ "GCS Logger: async_log_failure_event logging kwargs: %s, response_obj: %s",
+ kwargs,
+ response_obj,
+ )
+
+ start_time_str = start_time.strftime("%Y-%m-%d %H:%M:%S")
+ end_time_str = end_time.strftime("%Y-%m-%d %H:%M:%S")
+ headers = await self.construct_request_headers()
+
+ logging_payload: GCSBucketPayload = await self.get_gcs_payload(
+ kwargs, response_obj, start_time_str, end_time_str
+ )
+ logging_payload["log_event_type"] = "failed_api_call"
+
+ json_logged_payload = json.dumps(logging_payload)
+
+ # Get the current date
+ current_date = datetime.now().strftime("%Y-%m-%d")
+
+ # Modify the object_name to include the date-based folder
+ object_name = f"{current_date}/{uuid.uuid4().hex}"
+ response = await self.async_httpx_client.post(
+ headers=headers,
+ url=f"https://storage.googleapis.com/upload/storage/v1/b/{self.BUCKET_NAME}/o?uploadType=media&name={object_name}",
+ data=json_logged_payload,
+ )
+
+ if response.status_code != 200:
+ verbose_logger.error("GCS Bucket logging error: %s", str(response.text))
+
+ verbose_logger.debug("GCS Bucket response %s", response)
+ verbose_logger.debug("GCS Bucket status code %s", response.status_code)
+ verbose_logger.debug("GCS Bucket response.text %s", response.text)
+ except Exception as e:
+ verbose_logger.error("GCS Bucket logging error: %s", str(e))
async def construct_request_headers(self) -> Dict[str, str]:
from litellm import vertex_chat_completion
@@ -139,9 +185,18 @@ class GCSBucketLogger(CustomLogger):
optional_params=kwargs.get("optional_params", None),
)
response_dict = {}
- response_dict = convert_litellm_response_object_to_dict(
- response_obj=response_obj
- )
+ if response_obj:
+ response_dict = convert_litellm_response_object_to_dict(
+ response_obj=response_obj
+ )
+
+ exception_str = None
+
+ # Handle logging exception attributes
+ if "exception" in kwargs:
+ exception_str = kwargs.get("exception", "")
+ if not isinstance(exception_str, str):
+ exception_str = str(exception_str)
_spend_log_payload: SpendLogsPayload = get_logging_payload(
kwargs=kwargs,
@@ -156,8 +211,10 @@ class GCSBucketLogger(CustomLogger):
response_obj=response_dict,
start_time=start_time,
end_time=end_time,
- spend_log_metadata=_spend_log_payload["metadata"],
+ spend_log_metadata=_spend_log_payload.get("metadata", ""),
response_cost=kwargs.get("response_cost", None),
+ exception=exception_str,
+ log_event_type=None,
)
return gcs_payload
diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py
index cd7004e41d5..6a28d70b172 100644
--- a/litellm/proxy/spend_tracking/spend_tracking_utils.py
+++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py
@@ -21,6 +21,8 @@ def get_logging_payload(
if kwargs is None:
kwargs = {}
+ if response_obj is None:
+ response_obj = {}
# standardize this function to be used across, s3, dynamoDB, langfuse logging
litellm_params = kwargs.get("litellm_params", {})
metadata = (