@@ -1645,7 +1645,7 @@ async def run_async_with_artifact_delta(
16451645def test_agent_run_sse_yields_error_object_on_exception (
16461646 test_app , create_test_session , monkeypatch
16471647):
1648- """Test /run_sse streams an error object if streaming raises ."""
1648+ """Test /run_sse streams structured error details on exception ."""
16491649 info = create_test_session
16501650
16511651 async def run_async_raises (self , ** kwargs ):
@@ -1662,15 +1662,42 @@ async def run_async_raises(self, **kwargs):
16621662 "streaming" : True ,
16631663 }
16641664
1665- response = test_app .post ("/run_sse" , json = payload )
1666- assert response .status_code == 200
1667-
1668- sse_events = [
1669- json .loads (line .removeprefix ("data: " ))
1670- for line in response .text .splitlines ()
1671- if line .startswith ("data: " )
1672- ]
1673- assert sse_events == [{"error" : "boom" }]
1665+ # 1. Test without DEBUG enabled
1666+ with patch (
1667+ "google.adk.cli.api_server.logger.isEnabledFor" , return_value = False
1668+ ):
1669+ response = test_app .post ("/run_sse" , json = payload )
1670+ assert response .status_code == 200
1671+ sse_events = [
1672+ json .loads (line .removeprefix ("data: " ))
1673+ for line in response .text .splitlines ()
1674+ if line .startswith ("data: " )
1675+ ]
1676+ assert len (sse_events ) == 1
1677+ error_event = sse_events [0 ]
1678+ assert error_event ["error" ] == "ValueError: boom"
1679+ assert "error_details" in error_event
1680+ assert error_event ["error_details" ]["error_type" ] == "ValueError"
1681+ assert error_event ["error_details" ]["error_message" ] == "boom"
1682+ assert "stacktrace" not in error_event ["error_details" ]
1683+ assert "timestamp" in error_event ["error_details" ]
1684+
1685+ # 2. Test with DEBUG enabled
1686+ with patch (
1687+ "google.adk.cli.api_server.logger.isEnabledFor" , return_value = True
1688+ ):
1689+ response = test_app .post ("/run_sse" , json = payload )
1690+ assert response .status_code == 200
1691+ sse_events = [
1692+ json .loads (line .removeprefix ("data: " ))
1693+ for line in response .text .splitlines ()
1694+ if line .startswith ("data: " )
1695+ ]
1696+ assert len (sse_events ) == 1
1697+ error_event = sse_events [0 ]
1698+ assert error_event ["error" ] == "ValueError: boom"
1699+ assert "stacktrace" in error_event ["error_details" ]
1700+ assert "ValueError: boom" in error_event ["error_details" ]["stacktrace" ]
16741701
16751702
16761703def test_list_artifact_names (test_app , create_test_session ):
0 commit comments