Skip to content

Commit 81f1268

Browse files
authored
docs(graph): Extend graph traversal logic (#301)
* docs: clarify graph edge execution logic (OR vs AND) - Add note in 'How Graphs Work' explaining default OR behavior - Add 'Waiting for All Dependencies' subsection in Conditional Edges - Update parallel processing example to show AND logic pattern - Update best practices to mention edge execution logic * docs: revert parallel processing example to original Keep the parallel processing example simple without conditional edges. The AND logic pattern is already demonstrated in the Conditional Edges section. * fix: revert the best practice change
1 parent dcbf5c4 commit 81f1268

File tree

1 file changed

+23
-1
lines changed
  • docs/user-guide/concepts/multi-agent

1 file changed

+23
-1
lines changed

docs/user-guide/concepts/multi-agent/graph.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The Graph pattern operates on the principle of structured, deterministic workflo
1818
1. Nodes represent agents, custom nodes, or multi-agent systems
1919
2. Edges define dependencies and information flow between nodes
2020
3. Execution follows the graph structure, respecting dependencies
21+
1. When multiple nodes have edges to a target node, the target executes as soon as **any one** dependency completes. To enable more complex traversal use cases, see the [Conditional Edges](#conditional-edges) section.
2122
4. Output from one node becomes input for dependent nodes
2223
5. Entry points receive the original task as input
2324
6. Nodes can be revisited in cyclic patterns with proper exit conditions
@@ -137,7 +138,28 @@ def only_if_research_successful(state):
137138
builder.add_edge("research", "analysis", condition=only_if_research_successful)
138139
```
139140

140-
When multiple conditional edges converge on a single node, the target node executes as soon as any one of the incoming conditional edges is satisfied. The node doesn't wait for all predecessor nodes to complete, just the first one whose condition evaluates to true.
141+
### Waiting for All Dependencies
142+
143+
By default, when multiple nodes have edges to a target node, the target executes as soon as any one dependency completes. To wait for all dependencies to complete, use conditional edges that check all required nodes:
144+
145+
```python
146+
from strands.multiagent.graph import GraphState
147+
from strands.multiagent.base import Status
148+
149+
def all_dependencies_complete(required_nodes: list[str]):
150+
"""Factory function to create AND condition for multiple dependencies."""
151+
def check_all_complete(state: GraphState) -> bool:
152+
return all(
153+
node_id in state.results and state.results[node_id].status == Status.COMPLETED
154+
for node_id in required_nodes
155+
)
156+
return check_all_complete
157+
158+
# Z will only execute when A AND B AND C have all completed
159+
builder.add_edge("A", "Z", condition=all_dependencies_complete(["A", "B", "C"]))
160+
builder.add_edge("B", "Z", condition=all_dependencies_complete(["A", "B", "C"]))
161+
builder.add_edge("C", "Z", condition=all_dependencies_complete(["A", "B", "C"]))
162+
```
141163

142164
## Nested Multi-Agent Patterns
143165

0 commit comments

Comments
 (0)