From 4438e5201136450dacedd5713d933df27e2282b0 Mon Sep 17 00:00:00 2001 From: Aloys Jehwin Date: Wed, 23 Sep 2026 00:57:30 +0530 Subject: [PATCH] fix(spend): don't derive a SpendLogs id from a stringified-null provider id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A provider returning "id": null becomes the literal string "None" (str(None)) in standard_logging_object["id"]. In get_spend_logs_id that passed the truthy-string test and shadowed the unique litellm_call_id, so every null-id request got request_id="None" and collided on the SpendLogs primary key — skip_duplicates then dropped all but the first, silently. Treat "none"/"null" string sentinels as absent (a module-level frozenset) so resolution falls through to the unique per-call id. Fixes #39749 --- .../proxy/spend_tracking/spend_tracking_utils.py | 15 ++++++++++++++- .../spend_tracking/test_spend_tracking_utils.py | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index 8f85ecdd480..d7c7d28d54f 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -254,6 +254,9 @@ def _get_spend_logs_metadata( BATCH_COST_REQUEST_ID_SUFFIX: Final = "_batch_cost" +# str(None)/str(...) from a provider returning a null id; treated as absent so it +# never shadows the unique litellm_call_id when deriving the SpendLogs primary key. +_STRINGIFIED_NULL_IDS: Final = frozenset(("none", "null")) def get_spend_logs_id(call_type: str, response_obj: dict, kwargs: dict) -> str | None: @@ -263,8 +266,18 @@ def get_spend_logs_id(call_type: str, response_obj: dict, kwargs: dict) -> str | standard_logging_payload.get("id") if isinstance(standard_logging_payload, dict) else None, kwargs.get("litellm_call_id"), ) + # A provider returning "id": null becomes the literal string "None" in the + # logging payload (str(None)); it would pass the truthy-string test below, + # shadow the unique litellm_call_id, and collide on the SpendLogs primary + # key so every later null-id row is dropped by skip_duplicates. Treat those + # stringified-null sentinels as absent so the call id is used instead. resolved_id: Final = next( - (candidate for candidate in candidate_ids if isinstance(candidate, str) and candidate), None + ( + candidate + for candidate in candidate_ids + if isinstance(candidate, str) and candidate and candidate.strip().lower() not in _STRINGIFIED_NULL_IDS + ), + None, ) if resolved_id is not None and call_type == CallTypes.aretrieve_batch.value: return f"{resolved_id}{BATCH_COST_REQUEST_ID_SUFFIX}" diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py index f471e3f8fbb..8959cd385d6 100644 --- a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py +++ b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py @@ -4003,6 +4003,21 @@ def test_get_spend_logs_id_prefers_the_response_id_for_batch_and_file_calls(call assert get_spend_logs_id(call_type, {"id": "batch_abc123"}, {"litellm_call_id": "call-id-1"}) == expected +@pytest.mark.parametrize("null_id", ["None", "null", "none"]) +def test_get_spend_logs_id_falls_back_to_call_id_for_stringified_null_provider_id(null_id): + """A provider returning "id": null becomes the literal string str(None) == "None" + in the logging payload. That passed the truthy-string test, shadowed the unique + litellm_call_id, and made every null-id row collide on the SpendLogs primary key so + skip_duplicates dropped all but the first. Such sentinels must fall through to the + per-call id, which is unique.""" + first = get_spend_logs_id("acompletion", {"id": None}, {"standard_logging_object": {"id": null_id}, "litellm_call_id": "call-id-1"}) + second = get_spend_logs_id("acompletion", {"id": None}, {"standard_logging_object": {"id": null_id}, "litellm_call_id": "call-id-2"}) + + assert first == "call-id-1" + assert second == "call-id-2" + assert first != second + + def test_get_logging_payload_gives_redacted_batch_and_file_rows_distinct_request_ids(): """End to end at the payload level: a batch retrieve and a file create whose bodies were both flattened to the same redaction placeholder must still produce two