From f9d95b71bb6c1989571e4b05f02430341b19c74f Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Sat, 8 Nov 2025 10:07:40 -0800 Subject: [PATCH] fix _get_assembled_streaming_response --- litellm/litellm_core_utils/litellm_logging.py | 10 +++++----- .../test_openai_responses_api.py | 11 +++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 4fdd0dcdca..782f6dd225 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -3036,14 +3036,14 @@ class Logging(LiteLLMLoggingBaseClass): elif isinstance(result, ResponseCompletedEvent): ## return unified Usage object if isinstance(result.response.usage, ResponseAPIUsage): + transformed_usage = ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage( + result.response.usage + ) + # Set as dict instead of Usage object so model_dump() serializes it correctly setattr( result.response, "usage", - ( - ResponseAPILoggingUtils._transform_response_api_usage_to_chat_usage( - result.response.usage - ) - ), + transformed_usage.model_dump() if hasattr(transformed_usage, 'model_dump') else dict(transformed_usage), ) return result.response else: diff --git a/tests/llm_responses_api_testing/test_openai_responses_api.py b/tests/llm_responses_api_testing/test_openai_responses_api.py index 173860a0aa..c5c683f88a 100644 --- a/tests/llm_responses_api_testing/test_openai_responses_api.py +++ b/tests/llm_responses_api_testing/test_openai_responses_api.py @@ -65,7 +65,10 @@ def validate_standard_logging_payload( assert slp is not None, "Standard logging payload should not be None" # Validate token counts - print("response=", json.dumps(response, indent=4, default=str)) + print("VALIDATING STANDARD LOGGING PAYLOAD. response=", json.dumps(response, indent=4, default=str)) + print("FIELDS IN SLP=", json.dumps(slp, indent=4, default=str)) + print("SLP PROMPT TOKENS=", slp["prompt_tokens"]) + print("RESPONSE PROMPT TOKENS=", response["usage"]["input_tokens"]) assert ( slp["prompt_tokens"] == response["usage"]["input_tokens"] ), "Prompt tokens mismatch" @@ -108,7 +111,7 @@ async def test_basic_openai_responses_api_streaming_with_logging(): print("litellm response=", json.dumps(event, indent=4, default=str)) print("sleeping for 2 seconds...") - await asyncio.sleep(2) + await asyncio.sleep(4) print( "standard logging payload=", json.dumps(test_custom_logger.standard_logging_object, indent=4, default=str), @@ -135,11 +138,11 @@ def validate_responses_match(slp_response, litellm_response): # Validate usage assert ( - slp_response["usage"]["input_tokens"] + slp_response["usage"]["prompt_tokens"] == litellm_response["usage"]["input_tokens"] ), "Input tokens mismatch" assert ( - slp_response["usage"]["output_tokens"] + slp_response["usage"]["completion_tokens"] == litellm_response["usage"]["output_tokens"] ), "Output tokens mismatch" assert (