Overview
Thechain module provides a fluent API for composing nodes using the pipe operator (|).
Classes
ChainableNode
Base class for nodes that support chaining.Methods
or
Enables the pipe operator for chaining.other: Another chainable node
NodeChain: A chain containing both nodes
run
Execute the node with input data.input_data: Input data dictionary
- Output data dictionary
stream
Stream execution events.NodeChain
Container for chained nodes.Parameters
| Name | Type | Required | Description |
|---|---|---|---|
nodes | List[ChainableNode] | Yes | List of nodes to chain |
Methods
add_node
Add a node to the chain.execute
Execute all nodes in sequence.run
Run the chain with input data.FunctionNode
Wrap a function as a chainable node.Parameters
| Name | Type | Required | Description |
|---|---|---|---|
func | Callable | Yes | Function to wrap |
node_id | str | No | Node identifier |
name | str | No | Node name |
Supported Function Types
Synchronous Functions
Asynchronous Functions
Lambda Functions
PassthroughNode
Identity node that passes data through unchanged.Use Cases
- Debugging chains
- Placeholder nodes
- Data inspection points
TransformNode
Generic transformation node.Parameters
| Name | Type | Required | Description |
|---|---|---|---|
transform_fn | Callable | Yes | Transformation function |
node_id | str | No | Node identifier |
validate_fn | Callable | No | Validation function |
Utility Functions
create_chain
Create a chain from a list of nodes.chain_from_config
Create a chain from configuration.Composition Patterns
Simple Chain
Function Chain
Mixed Chain
Conditional Chain
Dynamic Chain Building
Advanced Features
Chain Inspection
Chain Cloning
Chain Composition
Error Handling
Chain Error Propagation
Performance Optimization
Lazy Chain Evaluation
Best Practices
Keep Chains Simple
Keep Chains Simple
Each chain should have a single, clear purpose. Complex workflows should be broken into sub-chains.
Use Type Hints
Use Type Hints
Handle Errors Gracefully
Handle Errors Gracefully
Always handle potential errors in chain execution to prevent cascading failures.
Test Chains Independently
Test Chains Independently
Test each chain component separately before testing the full chain.