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 _handle_logging_completed_response

Ishaan Jaffer9a022b82777c
diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py
index 3754959f6ab..8eecc3e8211 100644
--- a/litellm/responses/streaming_iterator.py
+++ b/litellm/responses/streaming_iterator.py
@@ -188,9 +188,15 @@ class ResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
def _handle_logging_completed_response(self):
"""Handle logging for completed responses in async context"""
+ # Create a deep copy for logging to avoid modifying the response object that will be returned to the user
+ # The logging handlers may transform usage from Responses API format (input_tokens/output_tokens)
+ # to chat completion format (prompt_tokens/completion_tokens) for internal logging
+ import copy
+ logging_response = copy.deepcopy(self.completed_response)
+
asyncio.create_task(
self.logging_obj.async_success_handler(
- result=self.completed_response,
+ result=logging_response,
start_time=self.start_time,
end_time=datetime.now(),
cache_hit=None,
@@ -199,7 +205,7 @@ class ResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
executor.submit(
self.logging_obj.success_handler,
- result=self.completed_response,
+ result=logging_response,
cache_hit=None,
start_time=self.start_time,
end_time=datetime.now(),
@@ -258,9 +264,15 @@ class SyncResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
def _handle_logging_completed_response(self):
"""Handle logging for completed responses in sync context"""
+ # Create a deep copy for logging to avoid modifying the response object that will be returned to the user
+ # The logging handlers may transform usage from Responses API format (input_tokens/output_tokens)
+ # to chat completion format (prompt_tokens/completion_tokens) for internal logging
+ import copy
+ logging_response = copy.deepcopy(self.completed_response)
+
run_async_function(
async_function=self.logging_obj.async_success_handler,
- result=self.completed_response,
+ result=logging_response,
start_time=self.start_time,
end_time=datetime.now(),
cache_hit=None,
@@ -268,7 +280,7 @@ class SyncResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
executor.submit(
self.logging_obj.success_handler,
- result=self.completed_response,
+ result=logging_response,
cache_hit=None,
start_time=self.start_time,
end_time=datetime.now(),