fix(spend): don't derive a SpendLogs id from a stringified-null provider id

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
This commit is contained in:
Aloys Jehwin 2026-09-23 00:57:30 +05:30
parent 2ea43214c9
commit 4438e52011
2 changed files with 29 additions and 1 deletions

View file

@ -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}"

View file

@ -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