fix: Stream errors as simple JSON objects in ADK web server SSE

The ADK web server's /run_sse endpoint now yields a JSON object like {"error": "..."} when an exception occurs during event generation. The adk_web_server_client is updated to detect this error payload and raise a RuntimeError.

Close #4291

Co-authored-by: George Weale <gweale@google.com>
PiperOrigin-RevId: 863475838
This commit is contained in:
George Weale
2026-01-30 18:18:31 -08:00
committed by Copybara-Service
parent d0102ecea3
commit 798d0053c8
4 changed files with 84 additions and 11 deletions
+42
View File
@@ -1019,6 +1019,48 @@ def test_agent_run_sse_splits_artifact_delta(
assert sse_events[1]["actions"]["artifactDelta"] == {"artifact.txt": 0}
def test_agent_run_sse_yields_error_object_on_exception(
test_app, create_test_session, monkeypatch
):
"""Test /run_sse streams an error object if streaming raises."""
info = create_test_session
async def run_async_raises(
self,
*,
user_id: str,
session_id: str,
invocation_id: Optional[str] = None,
new_message: Optional[types.Content] = None,
state_delta: Optional[dict[str, Any]] = None,
run_config: Optional[RunConfig] = None,
):
del user_id, session_id, invocation_id, new_message, state_delta, run_config
raise ValueError("boom")
if False: # pylint: disable=using-constant-test
yield _event_1()
monkeypatch.setattr(Runner, "run_async", run_async_raises)
payload = {
"app_name": info["app_name"],
"user_id": info["user_id"],
"session_id": info["session_id"],
"new_message": {"role": "user", "parts": [{"text": "Hello agent"}]},
"streaming": True,
}
response = test_app.post("/run_sse", json=payload)
assert response.status_code == 200
sse_events = [
json.loads(line.removeprefix("data: "))
for line in response.text.splitlines()
if line.startswith("data: ")
]
assert sse_events == [{"error": "boom"}]
def test_list_artifact_names(test_app, create_test_session):
"""Test listing artifact names for a session."""
info = create_test_session