From 8a989cc795859a6be0c79b77c4865920bbceb5bd Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 02:42:12 -0700 Subject: [PATCH] fix(a2a): ignore a null error field on streams and run the stream per-token billing test A stream event whose error field is null no longer zeroes the call's cost. The integration test for per-token stream billing now runs instead of skipping, and the URL agent SDK case skips when the optional a2a-sdk is not installed. --- litellm/a2a_protocol/streaming_iterator.py | 2 +- tests/integration/spend/test_agent_spend.py | 5 ----- .../a2a_protocol/test_a2a_streaming_iterator.py | 13 ++++++++++--- tests/unit/a2a_protocol/test_main.py | 11 +++++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/litellm/a2a_protocol/streaming_iterator.py b/litellm/a2a_protocol/streaming_iterator.py index 7900eea86e2..a121454fe2c 100644 --- a/litellm/a2a_protocol/streaming_iterator.py +++ b/litellm/a2a_protocol/streaming_iterator.py @@ -79,7 +79,7 @@ class A2AStreamingIterator: # Store chunk self.chunks.append(chunk) payload: Final = _chunk_payload(chunk) - self.saw_error = self.saw_error or "error" in payload + self.saw_error = self.saw_error or payload.get("error") is not None # Extract text from chunk for token counting self._collect_text_from_chunk(payload) diff --git a/tests/integration/spend/test_agent_spend.py b/tests/integration/spend/test_agent_spend.py index 58fcd4e9de2..fca848fbb43 100644 --- a/tests/integration/spend/test_agent_spend.py +++ b/tests/integration/spend/test_agent_spend.py @@ -269,9 +269,4 @@ def test_a2a_send_bills_input_and_output_tokens_at_the_agent_per_token_prices(ga def test_a2a_stream_bills_input_and_output_tokens_at_the_agent_per_token_prices(gateway: Gateway) -> None: - pytest.skip( - "BUG: message/stream to an agent priced with input_cost_per_token/output_cost_per_token logs spend 0.0 " - "with prompt_tokens 0 and completion_tokens 0, because A2AStreamingIterator reads text from the request " - "Part RootModels without dumping them and ignores artifact-update text, while message/send bills 1.125" - ) _assert_a2a_calls_are_billed(gateway, _PER_TOKEN, _PER_TOKEN_COST, (("message/stream", "/a2a/{agent}"),)) diff --git a/tests/unit/a2a_protocol/test_a2a_streaming_iterator.py b/tests/unit/a2a_protocol/test_a2a_streaming_iterator.py index 60f5c5a338b..0d4103ae979 100644 --- a/tests/unit/a2a_protocol/test_a2a_streaming_iterator.py +++ b/tests/unit/a2a_protocol/test_a2a_streaming_iterator.py @@ -5,6 +5,7 @@ async_success_handler (cross-thread pydantic mutation segfaults pydantic-core). """ import asyncio +import importlib.util import time from collections.abc import AsyncIterator, Callable, Mapping from datetime import datetime @@ -12,7 +13,6 @@ from typing import Final from types import SimpleNamespace import pytest -from a2a.compat.v0_3.types import SendStreamingMessageResponse import litellm from litellm.a2a_protocol import streaming_iterator as a2a_streaming_iterator_module @@ -272,7 +272,9 @@ async def _artifact_without_id_stream() -> AsyncIterator[Mapping[str, object]]: yield _status_event("completed") -async def _url_agent_sdk_stream() -> AsyncIterator[SendStreamingMessageResponse]: +async def _url_agent_sdk_stream() -> AsyncIterator[object]: + from a2a.compat.v0_3.types import SendStreamingMessageResponse + for event in ( _status_event("working", "Looking that up"), _artifact_event(_REPLY, append=False), @@ -289,7 +291,12 @@ async def _url_agent_sdk_stream() -> AsyncIterator[SendStreamingMessageResponse] pytest.param(_two_artifact_stream, f"{_FIRST_ARTIFACT} {_SECOND_ARTIFACT}", id="two_artifacts"), pytest.param(_status_message_stream, _REPLY, id="status_message_reply"), pytest.param(_artifact_without_id_stream, _REPLY, id="artifact_without_id"), - pytest.param(_url_agent_sdk_stream, _REPLY, id="url_agent_sdk_events"), + pytest.param( + _url_agent_sdk_stream, + _REPLY, + id="url_agent_sdk_events", + marks=pytest.mark.skipif(importlib.util.find_spec("a2a") is None, reason="needs the optional a2a-sdk"), + ), ), ) @pytest.mark.asyncio diff --git a/tests/unit/a2a_protocol/test_main.py b/tests/unit/a2a_protocol/test_main.py index cf653d16b97..ca5c7c61bd5 100644 --- a/tests/unit/a2a_protocol/test_main.py +++ b/tests/unit/a2a_protocol/test_main.py @@ -1009,14 +1009,17 @@ async def test_agentcore_send_bills_a_task_reply_at_the_agents_token_prices( _assert_billed_at_agent_prices(spend, a2a_spend_recorder, _per_token_cost, _LONG_AGENT_REPLY) +@pytest.mark.parametrize( + "extra_fields", + (pytest.param({}, id="result_only"), pytest.param({"error": None}, id="null_error_field")), +) @pytest.mark.asyncio async def test_agentcore_stream_bills_every_appended_reply_chunk( - a2a_spend_recorder: _A2ASpendRecorder, respx_reachable_httpx: None + extra_fields: Mapping[str, object], a2a_spend_recorder: _A2ASpendRecorder, respx_reachable_httpx: None ) -> None: + events: Final = tuple({**event, **extra_fields} for event in _agentcore_appended_chunks(_LONG_AGENT_REPLY, 40)) with respx.mock(assert_all_called=True) as router: - router.post(url__regex=r".*/invocations.*").mock( - return_value=_sse(*_agentcore_appended_chunks(_LONG_AGENT_REPLY, chunk_size=40)) - ) + router.post(url__regex=r".*/invocations.*").mock(return_value=_sse(*events)) await _drain_bridge_stream({**_AGENTCORE_PARAMS, **_PER_TOKEN_PRICES}) spend: Final = await a2a_spend_recorder.logged_spend()