diff --git a/src/google/adk/agents/context.py b/src/google/adk/agents/context.py index dac4a517d60..65a24c22e72 100644 --- a/src/google/adk/agents/context.py +++ b/src/google/adk/agents/context.py @@ -371,6 +371,13 @@ def output(self) -> Any: @output.setter def output(self, value: Any) -> None: if self._output_value is not None: + if self._output_value is value: + return + try: + if self._output_value == value: + return + except Exception: + pass raise ValueError( 'Output already set. A node can produce at most one output.' ) diff --git a/src/google/adk/workflow/_dynamic_node_scheduler.py b/src/google/adk/workflow/_dynamic_node_scheduler.py index ed24bd49b54..fe2a8119850 100644 --- a/src/google/adk/workflow/_dynamic_node_scheduler.py +++ b/src/google/adk/workflow/_dynamic_node_scheduler.py @@ -849,6 +849,15 @@ async def run_node_internal( resume_inputs=resume_inputs, ) + if ( + use_as_output + and not isinstance(ctx.node, Workflow) + and child_ctx + and child_ctx.output is not None + ): + if ctx.output is None: + ctx.output = child_ctx.output + transfer_to_agent = child_ctx.actions.transfer_to_agent if child_ctx else None if child_ctx and child_ctx.actions.skip_summarization: ctx.actions.skip_summarization = True diff --git a/tests/unittests/workflow/test_workflow_dynamic_nodes.py b/tests/unittests/workflow/test_workflow_dynamic_nodes.py index 805e7dc0ae0..5455f4262ab 100644 --- a/tests/unittests/workflow/test_workflow_dynamic_nodes.py +++ b/tests/unittests/workflow/test_workflow_dynamic_nodes.py @@ -767,6 +767,35 @@ async def parent(*, ctx, node_input): assert len(parent_outputs) == 0 +@pytest.mark.asyncio +async def test_dynamic_node_use_as_output_propagates_to_downstream_node(): + """Dynamic child with use_as_output=True sets parent ctx.output for downstream nodes. + + Regression test for #8183: Calling await ctx.run_node(..., use_as_output=True) + without an explicit return must populate the parent node's output so that + downstream workflow steps receive the delegated value. + """ + + @node(rerun_on_resume=True) + async def child(ctx, node_input): + return '10 folders' + + @node(rerun_on_resume=True) + async def dispatch(ctx, node_input): + await ctx.run_node(child, node_input, use_as_output=True) + + def receipt(node_input: str | None = None) -> str: + return 'Packed: ' + (node_input or 'MISSING_ORDER') + + wf = Workflow(name='wf_delegated', edges=[(START, dispatch, receipt)]) + ss = InMemorySessionService() + runner = create_runner(app_name='test', node=wf, session_service=ss) + session = await ss.create_session(app_name='test', user_id='u') + events = await _run(runner, ss, session, 'Please pack 10 folders.') + outputs = _outputs(events) + assert any('Packed: 10 folders' in str(o) for o in outputs) + + # ========================================================================= # None-output completion after interrupt # =========================================================================