Skip to content

Commit bccc274

Browse files
authored
docs(graph): Add docs for custom nodes (#188)
1 parent 39a6dea commit bccc274

File tree

1 file changed

+55
-2
lines changed
  • docs/user-guide/concepts/multi-agent

1 file changed

+55
-2
lines changed

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Graph Multi-Agent Pattern
22

3-
A Graph is a deterministic Directed Acyclic Graph (DAG) based agent orchestration system where agents or other multi-agent systems (like [Swarm](./swarm.md) or nested Graphs) are nodes in a graph. Nodes are executed according to edge dependencies, with output from one node passed as input to connected nodes.
3+
A Graph is a deterministic Directed Acyclic Graph (DAG) based agent orchestration system where agents, custom nodes, or other multi-agent systems (like [Swarm](./swarm.md) or nested Graphs) are nodes in a graph. Nodes are executed according to edge dependencies, with output from one node passed as input to connected nodes.
44

55
- **Deterministic execution order** based on DAG structure
66
- **Output propagation** along edges between nodes
77
- **Clear dependency management** between agents
88
- **Supports nested patterns** (Graph as a node in another Graph)
9+
- **Custom node types** for deterministic business logic and hybrid workflows
910
- **Conditional edge traversal** for dynamic workflows
1011
- **Multi-modal input support** for handling text, images, and other content types
1112

1213
## How Graphs Work
1314

1415
The Graph pattern operates on the principle of structured, deterministic workflows where:
1516

16-
1. Nodes represent agents or multi-agent systems
17+
1. Nodes represent agents, custom nodes, or multi-agent systems
1718
2. Edges define dependencies and information flow between nodes
1819
3. Execution follows a topological sort of the graph
1920
4. Output from one node becomes input for dependent nodes
@@ -160,6 +161,57 @@ result = graph("Research the impact of AI on healthcare and create a comprehensi
160161
print(f"\n{result}")
161162
```
162163

164+
## Custom Node Types
165+
166+
You can create custom node types by extending [`MultiAgentBase`](../../../api-reference/multiagent.md#strands.multiagent.base.MultiAgentBase) to implement deterministic business logic, data processing pipelines, and hybrid workflows.
167+
168+
```python
169+
from strands.multiagent.base import MultiAgentBase, NodeResult, Status, MultiAgentResult
170+
from strands.agent.agent_result import AgentResult
171+
from strands.types.content import ContentBlock, Message
172+
173+
class FunctionNode(MultiAgentBase):
174+
"""Execute deterministic Python functions as graph nodes."""
175+
176+
def __init__(self, func, name: str = None):
177+
super().__init__()
178+
self.func = func
179+
self.name = name or func.__name__
180+
181+
async def invoke_async(self, task, **kwargs):
182+
# Execute function and create AgentResult
183+
result = self.func(task if isinstance(task, str) else str(task))
184+
185+
agent_result = AgentResult(
186+
stop_reason="end_turn",
187+
message=Message(role="assistant", content=[ContentBlock(text=str(result))]),
188+
# ... metrics and state
189+
)
190+
191+
# Return wrapped in MultiAgentResult
192+
return MultiAgentResult(
193+
status=Status.COMPLETED,
194+
results={self.name: NodeResult(result=agent_result, ...)},
195+
# ... execution details
196+
)
197+
198+
# Usage example
199+
def validate_data(data):
200+
if not data.strip():
201+
raise ValueError("Empty input")
202+
return f"✅ Validated: {data[:50]}..."
203+
204+
validator = FunctionNode(func=validate_data, name="validator")
205+
builder.add_node(validator, "validator")
206+
```
207+
208+
Custom nodes enable:
209+
210+
- **Deterministic processing**: Guaranteed execution for business logic
211+
- **Performance optimization**: Skip LLM calls for deterministic operations
212+
- **Hybrid workflows**: Combine AI creativity with deterministic control
213+
- **Business rules**: Implement complex business logic as graph nodes
214+
163215
## Multi-Modal Input Support
164216

165217
Graphs support multi-modal inputs like text and images using [`ContentBlocks`](../../../api-reference/types.md#strands.types.content.ContentBlock):
@@ -374,3 +426,4 @@ builder.add_edge("business_specialist", "business_report")
374426
6. **Consider parallelism**: Independent branches can execute concurrently
375427
7. **Nest multi-agent patterns**: Use Swarms within Graphs for complex workflows
376428
8. **Leverage multi-modal inputs**: Use ContentBlocks for rich inputs including images
429+
9. **Create custom nodes for deterministic logic**: Use `MultiAgentBase` for business rules and data processing

0 commit comments

Comments
 (0)