From b49737f1f7a634be69b0c7225739318bf815b6bf Mon Sep 17 00:00:00 2001 From: Harshvardhan Kasliwal Date: Sat, 31 Oct 2026 23:32:07 +0530 Subject: [PATCH] fix(a2a): use LiteLLM's call id as the SpendLogs request_id A2A responses carry the JSON-RPC id the client chose, and both the standard logging payload and the SpendLogs row used it as the request id. SpendLogs request_id is the primary key and rows are inserted with skip_duplicates, so any call that reused a JSON-RPC id was silently dropped (284 of 1722 calls in the report) get_standard_logging_object_payload and get_spend_logs_id now prefer LiteLLM's own call id for send_message, asend_message and asend_message_streaming. The streaming path also builds its logging object with a fresh uuid as litellm_call_id and falls back to a uuid for the trace id instead of the JSON-RPC id --- litellm/a2a_protocol/main.py | 4 +- litellm/litellm_core_utils/litellm_logging.py | 7 ++- .../spend_tracking/spend_tracking_utils.py | 11 ++++- tests/unit/a2a_protocol/test_main.py | 22 +++++++++ .../test_litellm_logging.py | 47 +++++++++++++++++++ .../test_spend_tracking_utils.py | 13 +++++ 6 files changed, 99 insertions(+), 5 deletions(-) diff --git a/litellm/a2a_protocol/main.py b/litellm/a2a_protocol/main.py index dcbe7a6506e..7d1dd993eed 100644 --- a/litellm/a2a_protocol/main.py +++ b/litellm/a2a_protocol/main.py @@ -622,7 +622,7 @@ def _build_streaming_logging_obj( stream=False, call_type="asend_message_streaming", start_time=start_time, - litellm_call_id=str(request.id), + litellm_call_id=str(uuid.uuid4()), function_id=str(request.id), ) logging_obj.model = model @@ -765,7 +765,7 @@ async def asend_message_streaming( if api_base is None: raise ValueError("Either a2a_client or api_base is required for standard A2A flow") logging_trace_id: Final = getattr(logging_obj, "litellm_trace_id", None) if logging_obj else None - trace_id: Final = logging_trace_id or (str(request.id) if request.id else str(uuid.uuid4())) + trace_id: Final = logging_trace_id or str(uuid.uuid4()) extra_headers: Final[dict[str, str]] = {"X-LiteLLM-Trace-Id": trace_id} if agent_id: extra_headers["X-LiteLLM-Agent-Id"] = agent_id diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 639003c9a6a..8bc602fa2ec 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -7749,7 +7749,12 @@ def get_standard_logging_object_payload( else raw_usage_dict ) - id = response_obj.get("id") or kwargs.get("litellm_call_id") + # A2A responses carry the client-chosen JSON-RPC id, which is not unique + # across calls. Use LiteLLM's own call id so request_id stays unique. + if call_type in ("asend_message", "send_message", "asend_message_streaming"): + id = kwargs.get("litellm_call_id") or response_obj.get("id") + else: + 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/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py index ba5f873c4e9..b18615d9aab 100644 --- a/litellm/proxy/spend_tracking/spend_tracking_utils.py +++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py @@ -367,11 +367,18 @@ def get_spend_logs_id( call_type: str, response_obj: dict, kwargs: Mapping[str, object], metadata: Mapping[str, object] = EMPTY_MAPPING ) -> str | None: standard_logging_payload = kwargs.get("standard_logging_object") - candidate_ids: Final = ( - response_obj.get("id"), + response_id: Final = response_obj.get("id") + own_ids: Final = ( standard_logging_payload.get("id") if isinstance(standard_logging_payload, dict) else None, kwargs.get("litellm_call_id"), ) + # A2A responses carry the client-chosen JSON-RPC id, which is not unique + # across calls. Prefer LiteLLM's own ids so request_id stays unique. + candidate_ids: Final = ( + (*own_ids, response_id) + if call_type in ("asend_message", "send_message", "asend_message_streaming") + else (response_id, *own_ids) + ) resolved_id: Final = next( (candidate for candidate in candidate_ids if isinstance(candidate, str) and candidate), None ) diff --git a/tests/unit/a2a_protocol/test_main.py b/tests/unit/a2a_protocol/test_main.py index c0cabee1127..1604bb5b064 100644 --- a/tests/unit/a2a_protocol/test_main.py +++ b/tests/unit/a2a_protocol/test_main.py @@ -1302,3 +1302,25 @@ async def test_a2a_streaming(mock_a2a_client): assert len(chunks) > 0, "Should receive at least one chunk" print(f"\nTotal chunks received: {len(chunks)}") + + +def test_streaming_logging_obj_call_id_ignores_client_jsonrpc_id() -> None: + """Two streams that reuse a JSON-RPC id must get distinct litellm_call_ids.""" + from types import SimpleNamespace + + from litellm.a2a_protocol.main import _build_streaming_logging_obj + + call_ids: Final = [ + _build_streaming_logging_obj( + request=SimpleNamespace(id="1"), # type: ignore[arg-type] + agent_name="plain", + agent_id=None, + litellm_params=None, + metadata=None, + proxy_server_request=None, + start_time=datetime.datetime.now(), + ).litellm_call_id + for _ in range(2) + ] + assert call_ids[0] != call_ids[1] + assert "1" not in call_ids diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index f1854563cd5..7c63f7be2c2 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -14417,3 +14417,50 @@ def test_record_native_bedrock_guardrail_bills_each_call_its_own_trace_when_call (first_cost, second_cost) = costs assert second_cost == pytest.approx(2 * first_cost) assert shared_metadata == {"tag": "x"} + + +@pytest.mark.parametrize("call_type", ["asend_message", "send_message", "asend_message_streaming"]) +def test_a2a_payload_id_ignores_client_chosen_jsonrpc_id(logging_obj: Logging, call_type: str) -> None: + """Two A2A calls that reuse a JSON-RPC id must not share a standard logging payload id.""" + from litellm.litellm_core_utils.litellm_logging import get_standard_logging_object_payload + + now: Final = datetime_unit_test(2026, 1, 1, 12, 0, 0) + payload_ids: Final = [] + for call_id in ("litellm-call-a", "litellm-call-b"): + payload = get_standard_logging_object_payload( + kwargs={ + "model": "a2a_agent/test", + "call_type": call_type, + "litellm_call_id": call_id, + "litellm_params": {"metadata": {}}, + }, + init_response_obj=ModelResponse(id="1"), # JSON-RPC id chosen by the client + start_time=now, + end_time=now, + logging_obj=logging_obj, + status="success", + ) + assert payload is not None + payload_ids.append(payload["id"]) + assert payload_ids == ["litellm-call-a", "litellm-call-b"] + + +def test_non_a2a_payload_id_still_uses_response_id(logging_obj: Logging) -> None: + from litellm.litellm_core_utils.litellm_logging import get_standard_logging_object_payload + + now: Final = datetime_unit_test(2026, 1, 1, 12, 0, 0) + payload = get_standard_logging_object_payload( + kwargs={ + "model": "gpt-4o", + "call_type": "acompletion", + "litellm_call_id": "litellm-call-a", + "litellm_params": {"metadata": {}}, + }, + init_response_obj=ModelResponse(id="chatcmpl-123"), + start_time=now, + end_time=now, + logging_obj=logging_obj, + status="success", + ) + assert payload is not None + assert payload["id"] == "chatcmpl-123" diff --git a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py index f52f40ad55d..c6320c30f16 100644 --- a/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py +++ b/tests/unit/proxy/spend_tracking/test_spend_tracking_utils.py @@ -7587,3 +7587,16 @@ def test_spend_payload_preserves_unknown_completion_start(capture_enabled: bool, end if completion_start == "absent" else reported if completion_start else None ) assert observations == ([payload] if capture_enabled else []) + + +@pytest.mark.parametrize("call_type", ["asend_message", "send_message", "asend_message_streaming"]) +def test_get_spend_logs_id_ignores_client_chosen_jsonrpc_id_for_a2a_calls(call_type): + """A2A responses carry the JSON-RPC id the client picked, which is not unique across + calls. request_id is the LiteLLM_SpendLogs primary key and the flush inserts with + skip_duplicates, so two calls that reuse the same JSON-RPC id must still get + distinct request ids from LiteLLM's own call id.""" + first = get_spend_logs_id(call_type, {"id": "1"}, {"litellm_call_id": "call-id-1"}) + second = get_spend_logs_id(call_type, {"id": "1"}, {"litellm_call_id": "call-id-2"}) + + assert first == "call-id-1" + assert second == "call-id-2"