fix(spend): fall back to the call id when a provider returns a null response id

dict.get returns its default only for an absent key, so a response carrying
"id": null reached str() and logged the literal string "None" as its request
id. That id is the spend row's primary key and the writer inserts with
skip_duplicates, so the first such row landed and every one after it was
dropped without raising.

A provider id that is actually present still wins, unchanged.
This commit is contained in:
ChiFungHillmanChan 2026-09-14 00:25:35 +01:00
parent 2cad7a49af
commit 72c5e9e7ca
2 changed files with 28 additions and 1 deletions

View file

@ -6162,7 +6162,7 @@ def get_standard_logging_object_payload(
else raw_usage_dict
)
id = response_obj.get("id", kwargs.get("litellm_call_id"))
id = response_obj.get("id") or kwargs.get("litellm_call_id")
_model_id: Final = metadata.get("model_info", {}).get("id", "")
_model_group: Final = metadata.get("model_group", "")

View file

@ -3963,6 +3963,33 @@ def test_get_standard_logging_object_payload_includes_litellm_call_id(logging_ob
assert payload["litellm_call_id"] == call_id
def test_get_standard_logging_object_payload_falls_back_when_the_provider_id_is_null(logging_obj):
"""A null provider `id` falls back to the call id instead of logging the string "None"."""
import datetime
from litellm.litellm_core_utils.litellm_logging import (
get_standard_logging_object_payload,
)
call_id = "test-call-id-def-456"
now = datetime.datetime.now()
def payload_for(response_obj: dict):
payload = get_standard_logging_object_payload(
kwargs={"litellm_call_id": call_id, "model": "gpt-4o", "messages": []},
init_response_obj=response_obj,
start_time=now,
end_time=now,
logging_obj=logging_obj,
status="success",
)
assert payload is not None
return payload
assert payload_for({"id": None})["id"] == call_id
assert payload_for({"id": "chatcmpl-provider-id"})["id"] == "chatcmpl-provider-id"
def test_get_standard_logging_object_payload_carries_matched_access_groups(logging_obj):
"""Access groups stamped at auth time reach the logging payload, so integrations see what a request billed."""
from datetime import datetime