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 _get_assembled_streaming_response

Ishaan Jafferf9d95b71bb6c
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index 4fdd0dcdcaa..782f6dd2259 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 173860a0aae..c5c683f88a0 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 (