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]

fix(litellm_logging.py): skip should_run_logging check on streaming

Krrish Dholakia31a73be03f7f
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index f1d9f1ff1de..d6e37bba6f8 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -1175,16 +1175,30 @@ class Logging(LiteLLMLoggingBaseClass):
event_type: Literal[
"async_success", "sync_success", "async_failure", "sync_failure"
],
+ stream: bool = False,
) -> bool:
try:
if self.model_call_details.get(f"has_logged_{event_type}", False) is True:
return False
- self.model_call_details[f"has_logged_{event_type}"] = True
return True
except Exception:
return True
+ def has_run_logging(
+ self,
+ event_type: Literal[
+ "async_success", "sync_success", "async_failure", "sync_failure"
+ ],
+ ) -> None:
+ if self.stream is not None and self.stream is True:
+ """
+ Ignore check on stream, as there can be multiple chunks
+ """
+ return
+ self.model_call_details[f"has_logged_{event_type}"] = True
+ return
+
def should_run_callback(
self, callback: litellm.CALLBACK_TYPES, litellm_params: dict, event_hook: str
) -> bool:
@@ -1450,6 +1464,7 @@ class Logging(LiteLLMLoggingBaseClass):
call_type=self.call_type,
)
+ self.has_run_logging(event_type="sync_success")
for callback in callbacks:
try:
litellm_params = self.model_call_details.get("litellm_params", {})
@@ -1982,6 +1997,7 @@ class Logging(LiteLLMLoggingBaseClass):
call_type=self.call_type,
)
+ self.has_run_logging(event_type="async_success")
for callback in callbacks:
# check if callback can run for this request
litellm_params = self.model_call_details.get("litellm_params", {})
@@ -2226,6 +2242,7 @@ class Logging(LiteLLMLoggingBaseClass):
),
result=result,
)
+ self.has_run_logging(event_type="sync_failure")
for callback in callbacks:
try:
if callback == "lunary" and lunaryLogger is not None:
@@ -2406,6 +2423,7 @@ class Logging(LiteLLMLoggingBaseClass):
result = None # result sent to all loggers, init this to None incase it's not created
+ self.has_run_logging(event_type="async_failure")
for callback in callbacks:
try:
if isinstance(callback, CustomLogger): # custom logger class
diff --git a/tests/local_testing/test_custom_callback_input.py b/tests/local_testing/test_custom_callback_input.py
index bb0a7ef4258..dd26bf25fce 100644
--- a/tests/local_testing/test_custom_callback_input.py
+++ b/tests/local_testing/test_custom_callback_input.py
@@ -1536,7 +1536,7 @@ def test_logging_standard_payload_failure_call():
assert "additional_headers" in standard_logging_object["hidden_params"]
[email protected]("stream", [True, False])
[email protected]("stream", [False, True])
def test_logging_standard_payload_llm_headers(stream):
from litellm.types.utils import StandardLoggingPayload
diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
index 7744e34117a..facf0b98299 100644
--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
@@ -78,7 +78,8 @@ def test_logging_prevent_double_logging(logging_obj):
When using a bridge, log only once from the underlying bridge call.
This is to avoid double logging.
"""
- logging_obj.should_run_logging(event_type="sync_success")
+ logging_obj.stream = False
+ logging_obj.has_run_logging(event_type="sync_success")
assert logging_obj.should_run_logging(event_type="sync_success") == False
assert logging_obj.should_run_logging(event_type="sync_failure") == True
assert logging_obj.should_run_logging(event_type="async_success") == True