mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(proxy): log call IDs for request failures
This commit is contained in:
parent
629d7683f2
commit
f63f44d19b
2 changed files with 104 additions and 4 deletions
|
|
@ -1322,13 +1322,29 @@ def _has_attribute_error_in_chain(exc: Exception) -> bool:
|
|||
_CLIENT_DISCONNECT_DETAIL: Final = "Client disconnected the request"
|
||||
|
||||
|
||||
def _log_llm_api_exception(e: Exception) -> None:
|
||||
def _log_llm_api_exception(e: Exception, litellm_call_id: object | None = None) -> None:
|
||||
if getattr(e, "status_code", None) == 499 and getattr(e, "detail", None) == _CLIENT_DISCONNECT_DETAIL:
|
||||
if litellm_call_id is None:
|
||||
verbose_proxy_logger.info(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): client disconnected, upstream LLM request cancelled"
|
||||
)
|
||||
return
|
||||
verbose_proxy_logger.info(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): client disconnected, upstream LLM request cancelled"
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): "
|
||||
"client disconnected, upstream LLM request cancelled, litellm_call_id=%s",
|
||||
litellm_call_id,
|
||||
)
|
||||
return
|
||||
verbose_proxy_logger.exception("litellm.proxy.proxy_server._handle_llm_api_exception(): Exception occured - %s", e)
|
||||
if litellm_call_id is None:
|
||||
verbose_proxy_logger.exception(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): Exception occured - %s", e
|
||||
)
|
||||
return
|
||||
verbose_proxy_logger.exception(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): Exception occured - %s, litellm_call_id=%s",
|
||||
e,
|
||||
litellm_call_id,
|
||||
)
|
||||
|
||||
|
||||
async def _cancel_llm_call_on_client_disconnect(
|
||||
|
|
@ -3046,7 +3062,7 @@ class ProxyBaseLLMRequestProcessing:
|
|||
version: str | None = None,
|
||||
):
|
||||
"""Raises ProxyException (OpenAI API compatible) if an exception is raised"""
|
||||
_log_llm_api_exception(e)
|
||||
_log_llm_api_exception(e, litellm_call_id=self.data.get("litellm_call_id"))
|
||||
# Allow callbacks to transform the error response
|
||||
transformed_exception: Final = await proxy_logging_obj.post_call_failure_hook(
|
||||
user_api_key_dict=user_api_key_dict,
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ from litellm.proxy.common_request_processing import (
|
|||
_extract_error_from_sse_chunk,
|
||||
_get_cost_breakdown_from_logging_obj,
|
||||
_has_attribute_error_in_chain,
|
||||
_log_llm_api_exception,
|
||||
_is_azure_model_router_request,
|
||||
_UpstreamClosingStreamingResponse,
|
||||
open_sse_before_first_byte,
|
||||
|
|
@ -2658,6 +2659,89 @@ class TestHasAttributeErrorInChain:
|
|||
assert _has_attribute_error_in_chain(exc_a) is False
|
||||
|
||||
|
||||
class TestLogLLMApiException:
|
||||
def test_logs_call_id_without_request_data(self):
|
||||
error = TimeoutError("upstream timed out")
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.exception") as log_exception:
|
||||
_log_llm_api_exception(error, litellm_call_id="call-a")
|
||||
|
||||
log_exception.assert_called_once_with(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): "
|
||||
"Exception occured - %s, litellm_call_id=%s",
|
||||
error,
|
||||
"call-a",
|
||||
)
|
||||
|
||||
def test_preserves_log_format_when_call_id_is_missing(self):
|
||||
error = ValueError("upstream failed")
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.exception") as log_exception:
|
||||
_log_llm_api_exception(error)
|
||||
|
||||
log_exception.assert_called_once_with(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): Exception occured - %s", error
|
||||
)
|
||||
|
||||
def test_logs_distinct_call_ids_for_concurrent_failures(self):
|
||||
first_error = TimeoutError("first request timed out")
|
||||
second_error = TimeoutError("second request timed out")
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.exception") as log_exception:
|
||||
_log_llm_api_exception(first_error, litellm_call_id="call-first")
|
||||
_log_llm_api_exception(second_error, litellm_call_id="call-second")
|
||||
|
||||
assert [call.args[-1] for call in log_exception.call_args_list] == ["call-first", "call-second"]
|
||||
|
||||
def test_logs_call_id_for_client_disconnect(self):
|
||||
error = HTTPException(status_code=499, detail="Client disconnected the request")
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.info") as log_info:
|
||||
_log_llm_api_exception(error, litellm_call_id="call-disconnected")
|
||||
|
||||
log_info.assert_called_once_with(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): "
|
||||
"client disconnected, upstream LLM request cancelled, litellm_call_id=%s",
|
||||
"call-disconnected",
|
||||
)
|
||||
|
||||
def test_preserves_client_disconnect_log_format_when_call_id_is_missing(self):
|
||||
error = HTTPException(status_code=499, detail="Client disconnected the request")
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.info") as log_info:
|
||||
_log_llm_api_exception(error)
|
||||
|
||||
log_info.assert_called_once_with(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): "
|
||||
"client disconnected, upstream LLM request cancelled"
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handler_passes_request_call_id_without_request_body(self):
|
||||
error = TimeoutError("upstream timed out")
|
||||
processor = ProxyBaseLLMRequestProcessing(
|
||||
data={"litellm_call_id": "call-b", "messages": [{"content": "do-not-log"}]}
|
||||
)
|
||||
proxy_logging_obj = MagicMock(spec=ProxyLogging)
|
||||
proxy_logging_obj.post_call_failure_hook = AsyncMock(return_value=None)
|
||||
proxy_logging_obj.post_call_response_headers_hook = AsyncMock(return_value={})
|
||||
|
||||
with patch("litellm.proxy.common_request_processing.verbose_proxy_logger.exception") as log_exception:
|
||||
with pytest.raises(ProxyException):
|
||||
await processor._handle_llm_api_exception(
|
||||
e=error,
|
||||
user_api_key_dict=ProxyUserAPIKeyAuth(api_key="sk-test"),
|
||||
proxy_logging_obj=proxy_logging_obj,
|
||||
)
|
||||
|
||||
log_exception.assert_called_once_with(
|
||||
"litellm.proxy.proxy_server._handle_llm_api_exception(): "
|
||||
"Exception occured - %s, litellm_call_id=%s",
|
||||
error,
|
||||
"call-b",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestHandleLLMApiExceptionDictDetail:
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue