From e65936dae456f6153c5a84d658f93d788df6c94a Mon Sep 17 00:00:00 2001 From: Thomas Hutcheson Date: Mon, 22 Sep 2025 20:42:31 +0100 Subject: [PATCH 1/2] feat: Add documentation for early exit mechanism to SequentialAgent using escalate action #2999 --- .../workflow-agents/sequential-agents.md | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/agents/workflow-agents/sequential-agents.md b/docs/agents/workflow-agents/sequential-agents.md index a2f1e16a4..4f13c952e 100644 --- a/docs/agents/workflow-agents/sequential-agents.md +++ b/docs/agents/workflow-agents/sequential-agents.md @@ -21,6 +21,59 @@ When the `SequentialAgent`'s `Run Async` method is called, it performs the follo ![Sequential Agent](../../assets/sequential-agent.png){: width="600"} +### Early Exit with `exit_sequence` + +The `SequentialAgent` supports early termination when a sub-agent encounters a terminal condition that makes continuing the sequence unnecessary or impossible. + +#### When to use early exit: +- **Validation failures:** A validation agent detects critical errors +- **Empty results:** A search agent finds no data to process +- **Blocking conditions:** An agent hits an unrecoverable state +- **Conditional logic:** Based on input analysis, remaining steps aren't needed + +#### Usage + +Sub-agents can call the `exit_sequence` tool to terminate the sequence early: + +```python +from google.adk.tools import exit_sequence + +# In your agent's tool function +def validate_input(tool_context: ToolContext): + if critical_validation_fails: + exit_sequence(tool_context) # Stops the sequence here + return "Validation failed - terminating sequence" +``` + +#### Example: Search → Analysis → Report Pipeline + +```python +from google.adk.agents import LlmAgent, SequentialAgent +from google.adk.tools import exit_sequence, google_search + +search_agent = LlmAgent( + name="SearchAgent", + instruction="Search for results. If no useful results found, call exit_sequence.", + tools=[google_search, exit_sequence] +) + +analysis_agent = LlmAgent( + name="AnalysisAgent", + instruction="Analyze the search results." +) + +report_agent = LlmAgent( + name="ReportAgent", + instruction="Generate final report." +) + +# If search finds no results, analysis and report agents won't execute +pipeline = SequentialAgent(sub_agents=[search_agent, analysis_agent, report_agent]) +``` + +!!! note "Async Mode Only" + Early exit via `exit_sequence` is currently supported in async mode only. Live streaming mode uses the existing `task_completed()` mechanism for natural completion. + ### Full Example: Code Development Pipeline Consider a simplified code development pipeline: @@ -51,5 +104,3 @@ This ensures the code is written, *then* reviewed, and *finally* refactored, in ```java --8<-- "examples/java/snippets/src/main/java/agents/workflow/SequentialAgentExample.java:init" ``` - - From f5b8da6b4f133623430027d157598c6fbd8ec048 Mon Sep 17 00:00:00 2001 From: Thomas Hutcheson Date: Sun, 19 Oct 2025 17:24:22 +0100 Subject: [PATCH 2/2] docs: Update early exit mechanism documentation for SequentialAgent to include live mode support --- docs/agents/workflow-agents/sequential-agents.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/agents/workflow-agents/sequential-agents.md b/docs/agents/workflow-agents/sequential-agents.md index 4f13c952e..f4099b75d 100644 --- a/docs/agents/workflow-agents/sequential-agents.md +++ b/docs/agents/workflow-agents/sequential-agents.md @@ -71,8 +71,11 @@ report_agent = LlmAgent( pipeline = SequentialAgent(sub_agents=[search_agent, analysis_agent, report_agent]) ``` -!!! note "Async Mode Only" - Early exit via `exit_sequence` is currently supported in async mode only. Live streaming mode uses the existing `task_completed()` mechanism for natural completion. +!!! note "Live Mode Support" + Early exit via `exit_sequence` is supported in both async and live streaming modes. In live mode, the escalate action provides immediate termination, whilst `task_completed()` remains available for natural completion signals. + +!!! info "Similar to LoopAgent" + The `exit_sequence` tool works similarly to [`exit_loop`](loop-agents.md) in LoopAgent, providing a consistent early-exit pattern across workflow agents. ### Full Example: Code Development Pipeline