Skip to main content

WorkflowBuilder API

The WorkflowBuilder provides a fluent API for constructing workflows. Start by defining a start executor, add edges to connect executors, and build the workflow.

Creating Executors

Class-based Executors

Define executors by subclassing Executor and adding @handler methods:

Function-based Executors (Python)

For simple operations, use the @executor decorator:
Python

Explicit Type Parameters

When you need more control over types, specify them explicitly on the @handler decorator:
Python
With explicit types on @handler, introspection is disabled. You must specify all types via decorator parameters.

Edge Types

Simple Edges

Connect two executors directly:

Conditional Edges

Route messages based on conditions:

Fan-out Edges

Send messages to multiple executors in parallel:

Fan-in Edges

Aggregate messages from multiple sources:

Switch-Case Edges

Route to exactly one target based on ordered conditions:
Python
Switch-case evaluates cases in order and routes to the first match. Always include a Default case as the last option.

WorkflowContext

The WorkflowContext parameter in handlers provides methods for message passing and state management:

Sending Messages

Python

Yielding Outputs

Python

State Management

Python

WorkflowContext Type Parameters

WorkflowContext uses generic type parameters to define capabilities:
Python

Running Workflows

Basic Execution

Streaming Execution

Workflow Configuration

Maximum Iterations

Python

Workflow Name

Python

State Isolation Pattern

Wrap workflow construction in a function for clean state between runs:
Python
This pattern is recommended for reusable workflows. Each workflow instance maintains independent executor state.

Next Steps

Orchestration Patterns

Learn sequential, concurrent, and parallel execution patterns

Checkpoints

Save and restore workflow state