From a5b497667cf292a6d9dc6430dd45a0afe87b3357 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Sun, 16 Mar 2025 21:04:41 -0700 Subject: [PATCH] fix(logging_utils.py): revert change --- litellm/litellm_core_utils/logging_utils.py | 3 ++- .../litellm_core_utils/streaming_handler.py | 18 ++++++++++-------- .../test_assemble_streaming_responses.py | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/litellm/litellm_core_utils/logging_utils.py b/litellm/litellm_core_utils/logging_utils.py index c2d959b3c08..3c934a42761 100644 --- a/litellm/litellm_core_utils/logging_utils.py +++ b/litellm/litellm_core_utils/logging_utils.py @@ -77,7 +77,8 @@ def _assemble_complete_response_from_streaming_chunks( complete_streaming_response: Optional[ Union[ModelResponse, TextCompletionResponse] ] = None - if getattr(result, "usage", None) is not None: # if it's the last chunk + + if result.choices[0].finish_reason is not None: # if it's the last chunk streaming_chunks.append(result) try: complete_streaming_response = litellm.stream_chunk_builder( diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index 4d81cddf666..15d94b31a99 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -1555,10 +1555,11 @@ class CustomStreamWrapper: if response is None: continue ## LOGGING - threading.Thread( - target=self.run_success_logging_and_cache_storage, - args=(response, cache_hit), - ).start() # log response + executor.submit( + self.run_success_logging_and_cache_storage, + response, + cache_hit, + ) # log response choice = response.choices[0] if isinstance(choice, StreamingChoices): self.response_uptil_now += choice.delta.get("content", "") or "" @@ -1628,10 +1629,11 @@ class CustomStreamWrapper: usage = calculate_total_usage(chunks=self.chunks) processed_chunk._hidden_params["usage"] = usage ## LOGGING - threading.Thread( - target=self.run_success_logging_and_cache_storage, - args=(processed_chunk, cache_hit), - ).start() # log response + executor.submit( + self.run_success_logging_and_cache_storage, + processed_chunk, + cache_hit, + ) # log response return processed_chunk except Exception as e: traceback_exception = traceback.format_exc() diff --git a/tests/logging_callback_tests/test_assemble_streaming_responses.py b/tests/logging_callback_tests/test_assemble_streaming_responses.py index 7b28f69917e..1101350fa29 100644 --- a/tests/logging_callback_tests/test_assemble_streaming_responses.py +++ b/tests/logging_callback_tests/test_assemble_streaming_responses.py @@ -26,7 +26,7 @@ from respx import MockRouter import litellm from litellm import Choices, Message, ModelResponse, TextCompletionResponse, TextChoices -from litellm.litellm_core_utils.litellm_logging import ( +from litellm.litellm_core_utils.logging_utils import ( _assemble_complete_response_from_streaming_chunks, )