Skip to main content

Shared State

Shared state enables data sharing and communication between workflow nodes through the SharedState class and workflow_state() function.

Core Concept

Every workflow has an associated shared state that tracks execution history, stores data, and provides access to node outputs:

SharedState Parameter

Automatic Injection

Add a SharedState parameter to any node to access state:
Important: Only one SharedState parameter allowed per node.

State Access Methods

workflow_state() Function

Access state from decision functions and nested code:

Access Patterns

By node name:
By execution index:

Accessing Node Outputs

get_node_output()

Get output from previously executed nodes:

get_node_attribute()

Access nested attributes with dot notation:

State Properties

initial_input

Access the workflow’s original input:

final_output

Get the most recent node output:

previous_output

Access the immediately previous node’s output:

execution_count

Track how many nodes have executed:

Parallel Node Outputs

get_parallel_outputs()

Access results from parallel branches:

Dictionary-based Access

Parallel results automatically stored in state dict:

Execution History

get_execution_history()

Get full execution history:

get_node_execution_count()

Count how many times a node has executed:

get_execution_sequence()

Get sequence of executed node names:

Loop Detection

detect_execution_loop()

Detect repetitive execution patterns:

check_node_execution_limit()

Enforce execution limits per node:

Generic State Store

store Property

Store arbitrary workflow data:
Use Cases:
  • Accumulating metrics across nodes
  • Storing configuration data
  • Caching computed values
  • Temporary workflow-level storage

State Serialization

Save State

Persist state to disk:

Load State

Restore previously saved state:

to_dict() / from_dict()

Manual serialization:

Advanced Patterns

State-Based Routing

Accumulator Pattern

Conditional Processing

Metrics Collection

Best Practices

Performance Considerations

State Access Overhead

  • Dictionary access: Less than 1ms
  • get_node_output(): Less than 1ms (cached)
  • workflow_state(): Less than 1ms
  • Serialization: ~10ms per MB

Memory Impact

  • Per node output: ~100-500 bytes metadata
  • Execution history: ~50 bytes per entry
  • Store data: Depends on stored values

Optimization Tips

Error Handling

Missing Node Output

Invalid State Access

What’s Next?

Validation

Validate workflows before execution

Type Safety

Type checking for workflows

Reporting

Track workflow performance

Best Practices

Workflow design patterns