fix - working exception writing

This commit is contained in:
Ishaan Jaff 2024-04-30 12:48:17 -07:00
parent 22725bd44d
commit 06804bc70a

View file

@ -1217,6 +1217,59 @@ def cost_tracking():
litellm.success_callback.append(_PROXY_track_cost_callback) # type: ignore litellm.success_callback.append(_PROXY_track_cost_callback) # type: ignore
async def _PROXY_failure_handler(
kwargs, # kwargs to completion
completion_response: litellm.ModelResponse, # response from completion
start_time=None,
end_time=None, # start/end time for completion
):
global prisma_client
if prisma_client is not None:
verbose_proxy_logger.debug(
"inside _PROXY_failure_handler kwargs=", extra=kwargs
)
_exception = kwargs.get("exception")
traceback = kwargs.get("traceback")
_exception_type = _exception.__class__.__name__
_model = kwargs.get("model", None)
_status_code = _exception.status_code
_litellm_params = kwargs.get("litellm_params", {}) or {}
_metadata = _litellm_params.get("metadata", {}) or {}
_model_id = _metadata.get("model_info", {}).get("id", None)
verbose_proxy_logger.debug(
"\nexception_type",
_exception_type,
"\nrequest_model",
_model,
"\nmodel_id",
_model_id,
"\nexception",
_exception,
"\ntraceback",
traceback,
)
error_log = LiteLLM_ErrorLogs(
model_name=_model,
model_id=_model_id,
exception_type=_exception_type,
status_code=_status_code,
exception_string=str(_exception),
)
# helper function to convert to dict on pydantic v2 & v1
error_log_dict = _get_pydantic_json_dict(error_log)
error_log_dict["request_kwargs"] = json.dumps(error_log_dict["request_kwargs"])
await prisma_client.db.litellm_errorlogs.create(
data=error_log_dict # type: ignore
)
pass
async def _PROXY_track_cost_callback( async def _PROXY_track_cost_callback(
kwargs, # kwargs to completion kwargs, # kwargs to completion
completion_response: litellm.ModelResponse, # response from completion completion_response: litellm.ModelResponse, # response from completion
@ -1302,6 +1355,15 @@ async def _PROXY_track_cost_callback(
verbose_proxy_logger.debug("error in tracking cost callback - %s", e) verbose_proxy_logger.debug("error in tracking cost callback - %s", e)
def error_tracking():
global prisma_client, custom_db_client
if prisma_client is not None or custom_db_client is not None:
if isinstance(litellm.failure_callback, list):
verbose_proxy_logger.debug("setting litellm failure callback to track cost")
if (_PROXY_failure_handler) not in litellm.failure_callback: # type: ignore
litellm.failure_callback.append(_PROXY_failure_handler) # type: ignore
def _set_spend_logs_payload( def _set_spend_logs_payload(
payload: dict, prisma_client: PrismaClient, spend_logs_url: Optional[str] = None payload: dict, prisma_client: PrismaClient, spend_logs_url: Optional[str] = None
): ):
@ -3194,6 +3256,9 @@ async def startup_event():
## COST TRACKING ## ## COST TRACKING ##
cost_tracking() cost_tracking()
## Error Tracking ##
error_tracking()
db_writer_client = HTTPHandler() db_writer_client = HTTPHandler()
proxy_logging_obj._init_litellm_callbacks() # INITIALIZE LITELLM CALLBACKS ON SERVER STARTUP <- do this to catch any logging errors on startup, not when calls are being made proxy_logging_obj._init_litellm_callbacks() # INITIALIZE LITELLM CALLBACKS ON SERVER STARTUP <- do this to catch any logging errors on startup, not when calls are being made