From f149dea9de3e766f6f00918d016abfedfbb3d256 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:14:48 -0700 Subject: [PATCH] fix(spend): keep caller-set call ids from splitting the non-finite cost warning The overflow warning strips newlines from litellm_call_id, and neither non-finite cost warning prints the raw value, which said nothing beyond "non-finite" --- litellm/litellm_core_utils/core_helpers.py | 4 ++++ litellm/litellm_core_utils/litellm_logging.py | 3 ++- litellm/types/utils.py | 4 +--- .../test_litellm_logging.py | 20 +++++++++++-------- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 3f742688811..778ca84ce14 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -944,6 +944,10 @@ def finite_cost(value: object) -> float | None: return float(value) +def single_log_line(value: object) -> str: + return str(value).replace("\r", "").replace("\n", "") + + def finite_reported_cost(value: object) -> float | None: cost: Final = finite_cost(value) if cost is None and isinstance(value, float): diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index e401b59c826..c5075366051 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -84,6 +84,7 @@ from litellm.litellm_core_utils.core_helpers import ( proxy_stamped_used_client_oauth_token, reconstruct_model_name, set_response_cost_in_hidden_params, + single_log_line, ) from litellm.litellm_core_utils.error_normalization import normalize_error from litellm.litellm_core_utils.get_litellm_params import get_litellm_params @@ -2195,7 +2196,7 @@ class Logging(LiteLLMLoggingBaseClass): if cost is None or finite_cost(cost) is not None: return cost verbose_logger.warning( - "Recording a non-finite response_cost=%s as 0 for litellm_call_id=%s", cost, self.litellm_call_id + "Recording a non-finite response_cost as 0 for litellm_call_id=%s", single_log_line(self.litellm_call_id) ) return 0.0 diff --git a/litellm/types/utils.py b/litellm/types/utils.py index da67046c34b..bad2d05af14 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -2062,9 +2062,7 @@ class Usage(SafeAttributeModel, CompletionUsage): if cost is None: del self.cost elif isinstance(cost, float) and not math.isfinite(cost): - verbose_logger.warning( - "Dropping a non-finite provider usage cost %s, the cost map prices the request", cost - ) + verbose_logger.warning("Dropping a non-finite provider usage cost, the cost map prices the request") del self.cost else: self.cost = cost diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index 3c5603aefba..d2d7cb0fb48 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -6134,16 +6134,20 @@ def test_get_standard_logging_object_payload_logs_a_non_finite_response_cost_as_ assert payload["response_cost"] == 0 -def test_an_overflowing_custom_price_records_a_zero_response_cost() -> None: - response: Final = litellm.completion( - model="gpt-4o", - mock_response="hi there", - messages=[{"role": "user", "content": "hello world"}], - input_cost_per_token=1e308, - output_cost_per_token=1e308, - ) +def test_an_overflowing_custom_price_records_a_zero_response_cost(caplog: pytest.LogCaptureFixture) -> None: + with caplog.at_level(logging.WARNING, logger="LiteLLM"): + response: Final = litellm.completion( + model="gpt-4o", + mock_response="hi there", + messages=[{"role": "user", "content": "hello world"}], + input_cost_per_token=1e308, + output_cost_per_token=1e308, + litellm_call_id="overflow\nforged log line", + ) assert response._hidden_params["response_cost"] == 0.0 + warnings: Final = [r.getMessage() for r in caplog.records if "non-finite response_cost" in r.getMessage()] + assert warnings == ["Recording a non-finite response_cost as 0 for litellm_call_id=overflowforged log line"] def test_get_standard_logging_object_payload_carries_matched_access_groups(logging_obj):