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.
This commit is contained in:
mateo-berri 2026-10-30 02:42:12 -07:00
parent 1dda02d65b
commit 8a989cc795
4 changed files with 18 additions and 13 deletions

View file

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

View file

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

View file

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

View file

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