From 8f2124ecd8efc1c58a147d729124a357aec12bc9 Mon Sep 17 00:00:00 2001 From: TobiMayr Date: Thu, 9 Jul 2026 16:39:44 +0200 Subject: [PATCH] style(logging): dedupe transformed_usage dict computation; add missing test coverage Per style review: the model_dump()/dict() ternary for transformed_usage was computed twice (once for result.usage, once for standard_logging_payload["response"]["usage"]). Computed once into transformed_usage_dict and reused both places. Since result.usage is already set to this dict before response_dict = result.model_dump() runs, the second computation was fully redundant. Also trimmed the comment to match the terser tone used elsewhere in this file for the same kind of Pydantic-vs-dict note, and added a regression test for the standard_logging_payload branch, which had no existing coverage. --- .../test_litellm_logging.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index 3f2f1855f06..9f6c77d6af1 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -6216,6 +6216,34 @@ def test_transform_usage_objects_non_streaming_preserves_prompt_and_completion_t assert dumped_usage.get("total_tokens") == 150 +def test_transform_usage_objects_preserves_tokens_in_standard_logging_payload(): + response = ResponsesAPIResponse( + id="resp-test-002", + created_at=1700000000, + output=[], + usage=ResponseAPIUsage(input_tokens=100, output_tokens=50, total_tokens=150), + ) + + logging_obj = LitellmLogging( + model="gpt-5", + messages=[{"role": "user", "content": "Hello"}], + stream=False, + call_type="responses", + start_time=time.time(), + litellm_call_id="test-usage-preservation-standard-logging", + function_id="test-fn-usage-standard-logging", + ) + logging_obj.model_call_details["standard_logging_object"] = {} + + logging_obj._transform_usage_objects(response) + + standard_logging_payload = logging_obj.model_call_details["standard_logging_object"] + logged_usage = standard_logging_payload["response"]["usage"] + assert logged_usage.get("prompt_tokens") == 100 + assert logged_usage.get("completion_tokens") == 50 + assert logged_usage.get("total_tokens") == 150 + + def test_success_handler_preserves_precomputed_cost_for_dict_response(): """Precomputed response_cost on model_call_details must not be overwritten.""" logging_obj = _make_dict_logging_obj()