From 72c5e9e7ca27a3609f722dedce919e34d2fd9048 Mon Sep 17 00:00:00 2001 From: ChiFungHillmanChan Date: Mon, 14 Sep 2026 00:25:35 +0100 Subject: [PATCH] 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. --- litellm/litellm_core_utils/litellm_logging.py | 2 +- .../test_litellm_logging.py | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 9ba9fd082f3..e78eaa19b18 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -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", "") diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py index 70f9bae283b..362531eb982 100644 --- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py +++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py @@ -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