Skip to content

Commit 438367d

Browse files
committed
fix(plugins): Capture workflow node results in BigQuery
Emit NODE_OUTPUT and NODE_ERROR rows from workflow events that already reach the plugin, preserving node identity while avoiding message-as-output duplication. Fixes google#6529
1 parent 8994a02 commit 438367d

2 files changed

Lines changed: 187 additions & 0 deletions

File tree

‎src/google/adk/plugins/bigquery_agent_analytics_plugin.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,24 @@ def _parse_custom_metadata_allowlist(
37953795
"JSON_VALUE(attributes, '$.adk.pause_kind') AS pause_kind",
37963796
"JSON_VALUE(attributes, '$.adk.function_call_id') AS function_call_id",
37973797
],
3798+
"NODE_OUTPUT": [
3799+
"JSON_VALUE(attributes, '$.adk.node.path') AS node_path",
3800+
"JSON_VALUE(attributes, '$.adk.node.run_id') AS node_run_id",
3801+
(
3802+
"JSON_VALUE(attributes, '$.adk.node.parent_run_id')"
3803+
" AS node_parent_run_id"
3804+
),
3805+
"content AS output",
3806+
],
3807+
"NODE_ERROR": [
3808+
"JSON_VALUE(attributes, '$.adk.node.path') AS node_path",
3809+
"JSON_VALUE(attributes, '$.adk.node.run_id') AS node_run_id",
3810+
(
3811+
"JSON_VALUE(attributes, '$.adk.node.parent_run_id')"
3812+
" AS node_parent_run_id"
3813+
),
3814+
"JSON_VALUE(content, '$.error_code') AS error_code",
3815+
],
37983816
}
37993817

38003818
_VIEW_SQL_TEMPLATE = """\
@@ -6286,6 +6304,31 @@ async def on_event_callback(
62866304
),
62876305
)
62886306

6307+
node_path = getattr(getattr(event, "node_info", None), "path", "")
6308+
if node_path and event.error_code:
6309+
await self._log_event(
6310+
"NODE_ERROR",
6311+
callback_ctx,
6312+
raw_content={"error_code": event.error_code},
6313+
event_data=EventData(
6314+
source_event=event,
6315+
status="ERROR",
6316+
error_message=event.error_message,
6317+
),
6318+
)
6319+
elif (
6320+
node_path
6321+
and event.partial is not True
6322+
and event.content is None
6323+
and event.output is not None
6324+
):
6325+
await self._log_event(
6326+
"NODE_OUTPUT",
6327+
callback_ctx,
6328+
raw_content=event.output,
6329+
event_data=EventData(source_event=event),
6330+
)
6331+
62896332
# --- AGENT_TRANSFER ---
62906333
# actions.transfer_to_agent stores the *target* agent only
62916334
# (events/event_actions.py); from_agent is pinned to event.author

‎tests/unittests/plugins/test_bigquery_agent_analytics_plugin.py‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ async def test_on_event_callback_ignores_empty_state_delta(
20902090
bq_plugin_inst,
20912091
mock_write_client,
20922092
invocation_context,
2093+
dummy_arrow_schema,
20932094
):
20942095
"""on_event_callback should not log when state_delta is empty."""
20952096
event = event_lib.Event(
@@ -6779,6 +6780,16 @@ def test_llm_response_view_exposes_finish_reason(self):
67796780
"JSON_VALUE(attributes, '$.finish_reason') AS finish_reason" in columns
67806781
)
67816782

6783+
@pytest.mark.parametrize("event_type", ["NODE_OUTPUT", "NODE_ERROR"])
6784+
def test_node_views_expose_workflow_identity(self, event_type):
6785+
"""Workflow-node views expose stable node identity columns."""
6786+
columns = bigquery_agent_analytics_plugin._EVENT_VIEW_DEFS[event_type]
6787+
6788+
assert "JSON_VALUE(attributes, '$.adk.node.path') AS node_path" in columns
6789+
assert (
6790+
"JSON_VALUE(attributes, '$.adk.node.run_id') AS node_run_id" in columns
6791+
)
6792+
67826793
def test_config_create_views_default_true(self):
67836794
"""Config create_views defaults to True."""
67846795
config = bigquery_agent_analytics_plugin.BigQueryLoggerConfig()
@@ -9665,6 +9676,139 @@ async def test_route_and_rewind_flat_under_attributes_adk(
96659676
assert "actions" not in adk
96669677

96679678

9679+
class TestWorkflowNodeEvents:
9680+
"""Workflow node outputs and failures are observable through the plugin."""
9681+
9682+
@pytest.mark.asyncio
9683+
@pytest.mark.parametrize("output", [{"id": 7}, ["a", "b"], "done"])
9684+
async def test_node_output_preserves_payload_and_identity(
9685+
self,
9686+
output,
9687+
bq_plugin_inst,
9688+
mock_write_client,
9689+
invocation_context,
9690+
dummy_arrow_schema,
9691+
):
9692+
"""Function-node payloads produce one identity-bearing NODE_OUTPUT row."""
9693+
event = event_lib.Event(
9694+
author="step",
9695+
output=output,
9696+
node_info=event_lib.NodeInfo(path="wf@1/step@2"),
9697+
)
9698+
9699+
await bq_plugin_inst.on_event_callback(
9700+
invocation_context=invocation_context, event=event
9701+
)
9702+
await bq_plugin_inst.flush()
9703+
9704+
row = await _get_captured_event_dict_async(
9705+
mock_write_client, dummy_arrow_schema
9706+
)
9707+
assert row["event_type"] == "NODE_OUTPUT"
9708+
stored_output = (
9709+
json.loads(row["content"])
9710+
if isinstance(output, (dict, list))
9711+
else row["content"]
9712+
)
9713+
assert stored_output == output
9714+
node = json.loads(row["attributes"])["adk"]["node"]
9715+
assert node["path"] == "wf@1/step@2"
9716+
assert node["run_id"] == "2"
9717+
9718+
@pytest.mark.asyncio
9719+
async def test_output_and_state_delta_emit_separate_rows(
9720+
self,
9721+
bq_plugin_inst,
9722+
mock_write_client,
9723+
invocation_context,
9724+
dummy_arrow_schema,
9725+
):
9726+
"""A node event preserves both its state change and returned output."""
9727+
event = event_lib.Event(
9728+
author="step",
9729+
output={"result": 1},
9730+
actions=event_actions_lib.EventActions(state_delta={"count": 1}),
9731+
node_info=event_lib.NodeInfo(path="wf@1/step@2"),
9732+
)
9733+
9734+
await bq_plugin_inst.on_event_callback(
9735+
invocation_context=invocation_context, event=event
9736+
)
9737+
await bq_plugin_inst.flush()
9738+
9739+
rows = await _get_captured_rows_async(mock_write_client, dummy_arrow_schema)
9740+
assert [row["event_type"] for row in rows] == [
9741+
"STATE_DELTA",
9742+
"NODE_OUTPUT",
9743+
]
9744+
9745+
@pytest.mark.asyncio
9746+
async def test_node_error_uses_sanitized_error_column(
9747+
self,
9748+
bq_plugin_inst,
9749+
mock_write_client,
9750+
invocation_context,
9751+
dummy_arrow_schema,
9752+
):
9753+
"""Workflow failures produce an error row with their node identity."""
9754+
event = event_lib.Event(
9755+
author="step",
9756+
error_code="ValueError",
9757+
error_message="invalid input",
9758+
node_info=event_lib.NodeInfo(path="wf@1/step@2"),
9759+
)
9760+
9761+
await bq_plugin_inst.on_event_callback(
9762+
invocation_context=invocation_context, event=event
9763+
)
9764+
await bq_plugin_inst.flush()
9765+
9766+
row = await _get_captured_event_dict_async(
9767+
mock_write_client, dummy_arrow_schema
9768+
)
9769+
assert row["event_type"] == "NODE_ERROR"
9770+
assert row["status"] == "ERROR"
9771+
assert row["error_message"] == "invalid input"
9772+
assert json.loads(row["content"])["error_code"] == "ValueError"
9773+
9774+
@pytest.mark.asyncio
9775+
@pytest.mark.parametrize(
9776+
"event",
9777+
[
9778+
event_lib.Event(
9779+
author="step",
9780+
output=None,
9781+
node_info=event_lib.NodeInfo(path="wf@1/step@2"),
9782+
),
9783+
event_lib.Event(
9784+
author="agent",
9785+
content=types.Content(parts=[types.Part(text="answer")]),
9786+
output="answer",
9787+
node_info=event_lib.NodeInfo(
9788+
path="wf@1/agent@2", message_as_output=True
9789+
),
9790+
),
9791+
],
9792+
ids=("none", "message-as-output"),
9793+
)
9794+
async def test_non_output_events_do_not_duplicate_node_rows(
9795+
self,
9796+
event,
9797+
bq_plugin_inst,
9798+
mock_write_client,
9799+
invocation_context,
9800+
dummy_arrow_schema,
9801+
):
9802+
"""Empty and message-delegated events do not add NODE_OUTPUT rows."""
9803+
await bq_plugin_inst.on_event_callback(
9804+
invocation_context=invocation_context, event=event
9805+
)
9806+
await bq_plugin_inst.flush()
9807+
9808+
rows = await _get_captured_rows_async(mock_write_client, dummy_arrow_schema)
9809+
assert all(row["event_type"] != "NODE_OUTPUT" for row in rows)
9810+
9811+
96689812
class TestViewDefsRegistration:
96699813
"""The plugin's own per-event-type view defs cover the new types."""
96709814

0 commit comments

Comments
 (0)