From bd6f2edbf30a70b7c9fefc6305f3d40a8a157d4f Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Sat, 16 Aug 2025 14:25:29 -0700 Subject: [PATCH] fix(streaming_handler.py): fix streaming chunk calculation --- litellm/litellm_core_utils/streaming_handler.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index 3721851a38f..968a6aade98 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -1731,6 +1731,11 @@ class CustomStreamWrapper: # Create a new object without the removed attribute processed_chunk = self.model_response_creator(chunk=obj_dict) print_verbose(f"final returned processed chunk: {processed_chunk}") + + # add usage as hidden param + if self.sent_last_chunk is True and self.stream_options is None: + usage = calculate_total_usage(chunks=self.chunks) + processed_chunk._hidden_params["usage"] = usage return processed_chunk raise StopAsyncIteration else: # temporary patch for non-aiohttp async calls @@ -1774,6 +1779,7 @@ class CustomStreamWrapper: messages=self.messages, logging_obj=self.logging_obj, ) + response = self.model_response_creator() if complete_streaming_response is not None: setattr(