Skip to content

Commit ad560ce

Browse files
DeanChensjcopybara-github
authored andcommitted
fix: Default subagents to chat mode in build_node
* Dynamically attached subagents bypass LlmAgent.model_post_init normalization and retain mode=None. * Previously, build_node defaulted all mode=None agents to single_turn, preventing agent transfer and causing duplicate output errors. * Now, build_node checks for agent.parent_agent to identify subagents and defaults them to chat mode, preserving single_turn only for standalone nodes. Co-authored-by: Shangjie Chen <deanchen@google.com> PiperOrigin-RevId: 929471803
1 parent 4bd9454 commit ad560ce

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/google/adk/workflow/utils/_workflow_graph_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ def build_node(
9292
agent.parent_agent = node_like.parent_agent
9393

9494
if agent.mode is None:
95-
agent.mode = 'single_turn'
95+
# Sub-agents dynamically attached to a parent agent default to 'chat'
96+
# mode to enable agent transfer.
97+
# Standalone agents in a workflow graph default to 'single_turn'.
98+
if agent.parent_agent is not None:
99+
agent.mode = 'chat'
100+
else:
101+
agent.mode = 'single_turn'
96102

97103
if agent.mode in ('task', 'chat'):
98104
agent.wait_for_output = True

tests/unittests/workflow/utils/test_workflow_graph_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import annotations
1616

17+
from google.adk.agents.llm_agent import LlmAgent
1718
from google.adk.tools.base_tool import BaseTool
1819
from google.adk.workflow._base_node import BaseNode
1920
from google.adk.workflow._base_node import START
@@ -117,3 +118,19 @@ def test_raises_value_error_for_invalid_type(self):
117118
"""build_node raises ValueError for invalid types."""
118119
with pytest.raises(ValueError, match="Invalid node type"):
119120
build_node(123)
121+
122+
def test_llm_agent_mode_defaults(self):
123+
"""build_node sets correct default mode for LlmAgent."""
124+
root_agent = LlmAgent(name="root", instruction="test")
125+
sub_agent = LlmAgent(name="sub", description="test")
126+
# Dynamic subagent attachment without model_post_init normalization
127+
sub_agent.parent_agent = root_agent
128+
129+
# Subagent with parent_agent should default to chat mode
130+
built_sub = build_node(sub_agent)
131+
assert built_sub.mode == "chat"
132+
133+
# Standalone agent without parent_agent should default to single_turn
134+
standalone = LlmAgent(name="standalone", instruction="test")
135+
built_standalone = build_node(standalone)
136+
assert built_standalone.mode == "single_turn"

0 commit comments

Comments
 (0)