This commit is contained in:
Harshvardhan Kasliwal 2026-11-02 04:20:09 +05:30 • committed by GitHub
commit 9bfcc190da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 99 additions and 5 deletions

View file

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

View file

@ -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", "")

View file

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

View file

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

View file

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

View file

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