Workflow
workflow ¶
Workflow agent utilities for gepa-adk.
This module provides utilities for detecting, traversing, and cloning ADK workflow agents (SequentialAgent, LoopAgent, ParallelAgent) to enable workflow-level evolution of nested LlmAgents.
Examples:
Detecting workflow agents:
from google.adk.agents import SequentialAgent, LlmAgent
from gepa_adk.adapters.workflow import is_workflow_agent
agent = SequentialAgent(name="Pipeline", sub_agents=[])
assert is_workflow_agent(agent) == True
llm_agent = LlmAgent(name="Agent", instruction="Be helpful")
assert is_workflow_agent(llm_agent) == False
Finding LlmAgents in a workflow:
from gepa_adk.adapters.workflow import find_llm_agents
agents = find_llm_agents(workflow, max_depth=5)
print(f"Found {len(agents)} LlmAgents")
Cloning workflows with instruction overrides:
from gepa_adk.adapters.workflow import clone_workflow_with_overrides
candidate = {"agent1.instruction": "New instruction"}
cloned = clone_workflow_with_overrides(workflow, candidate)
# cloned preserves structure (LoopAgent iterations, ParallelAgent concurrency)
Note
This module isolates ADK-specific imports to the adapters layer, following hexagonal architecture principles (ADR-000).
is_workflow_agent ¶
Check if an agent is a workflow type.
Detects whether an agent is a workflow agent (SequentialAgent, LoopAgent, or ParallelAgent) versus a regular LlmAgent or other agent type.
| PARAMETER | DESCRIPTION |
|---|---|
agent | Agent instance to check. Can be any object type. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | True if agent is SequentialAgent, LoopAgent, or ParallelAgent. |
bool | False otherwise (including LlmAgent, None, or non-agent objects). |
Examples:
Detecting workflow agents:
from google.adk.agents import SequentialAgent, LlmAgent
from gepa_adk.adapters.workflow import is_workflow_agent
sequential = SequentialAgent(name="Pipeline", sub_agents=[])
assert is_workflow_agent(sequential) is True
llm = LlmAgent(name="Agent", instruction="Be helpful")
assert is_workflow_agent(llm) is False
Note
Only workflow agent types (SequentialAgent, LoopAgent, ParallelAgent) are detected. All workflow agents inherit from BaseAgent and have sub_agents, but type detection uses specific class checks for accuracy.
Source code in src/gepa_adk/adapters/workflow.py
find_llm_agents ¶
Find all LlmAgents in a workflow (recursive traversal with depth limiting).
Traverses a workflow agent structure recursively to discover all LlmAgent instances at any nesting level, up to the specified maximum depth.
| PARAMETER | DESCRIPTION |
|---|---|
agent | Agent or workflow to search. Can be LlmAgent, workflow agent, or any object. TYPE: |
max_depth | Maximum recursion depth (default: 5). When current_depth reaches max_depth, traversal stops. Must be >= 1 for meaningful results. TYPE: |
current_depth | Current recursion level (internal use, default: 0). TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
list['LlmAgent'] | List of LlmAgent instances found. Only includes agents with string |
list['LlmAgent'] | instructions (skips InstructionProvider callables). |
Examples:
Finding LlmAgents in a SequentialAgent:
from google.adk.agents import LlmAgent, SequentialAgent
from gepa_adk.adapters.workflow import find_llm_agents
agent1 = LlmAgent(name="agent1", instruction="First")
agent2 = LlmAgent(name="agent2", instruction="Second")
workflow = SequentialAgent(name="pipeline", sub_agents=[agent1, agent2])
agents = find_llm_agents(workflow)
assert len(agents) == 2
Finding LlmAgents in nested workflows:
# Sequential -> Parallel -> LlmAgents
nested_parallel = ParallelAgent(name="parallel", sub_agents=[agent2, agent3])
workflow = SequentialAgent(
name="pipeline", sub_agents=[agent1, nested_parallel]
)
agents = find_llm_agents(workflow, max_depth=5)
assert len(agents) == 3 # Finds all agents across levels
Note
Operates recursively with depth limiting to discover nested LlmAgents. Skips LlmAgents with InstructionProvider callables (non-string instructions). Respects max_depth to prevent infinite recursion.
Source code in src/gepa_adk/adapters/workflow.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
clone_workflow_with_overrides ¶
Clone workflow with instruction overrides applied to LlmAgent leaves.
Recursively clones a workflow structure, preserving all workflow agent properties (LoopAgent.max_iterations, ParallelAgent concurrency, etc.) while applying instruction overrides from the candidate dict to LlmAgents.
| PARAMETER | DESCRIPTION |
|---|---|
workflow | Original workflow to clone. Can be LlmAgent, SequentialAgent, LoopAgent, or ParallelAgent. TYPE: |
candidate | Qualified component name to text mapping. Keys should follow the pattern TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
AnyAgentType | Cloned workflow with same structure and type as input. LlmAgents have |
AnyAgentType | instruction overrides applied. Container agents (Sequential, Loop, |
AnyAgentType | Parallel) preserve all properties including max_iterations. |
Invariants
- type(result) == type(workflow)
- For LoopAgent: result.max_iterations == workflow.max_iterations
- len(result.sub_agents) == len(workflow.sub_agents) for all workflows
Examples:
Cloning with instruction overrides:
from gepa_adk.adapters.workflow import clone_workflow_with_overrides
agent = LlmAgent(name="writer", instruction="Original")
candidate = {"writer.instruction": "New instruction"}
cloned = clone_workflow_with_overrides(agent, candidate)
assert cloned.instruction == "New instruction"
assert agent.instruction == "Original" # Original unchanged
Cloning LoopAgent preserves iterations:
loop = LoopAgent(name="refine", sub_agents=[inner], max_iterations=3)
cloned = clone_workflow_with_overrides(loop, {})
assert cloned.max_iterations == 3 # Preserved!
Note
Only instruction components are applied during cloning. Other components (output_schema, generate_content_config) must be applied separately via _apply_candidate() before cloning.
Source code in src/gepa_adk/adapters/workflow.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |