Skip to main content

Workflow Control Flow

DurableWorkflow is the single durable workflow primitive. Use it for one saved step or for a pipeline with parallel branches, joins, bounded map work, JSON-based conditions, loops, approval gates, and cleanup after failure. Every SDK sends the same workflow description to the shared runtime. Routing, combining results, scheduling, loop limits, cleanup order, and replay are consistent across languages. Your callbacks perform the named work for each step.

Use this page when routes follow application rules or workflow state. If a step needs model reasoning, run an Agent inside that step and pass its structured result into the workflow condition. This keeps routing reproducible and inspectable while allowing model reasoning where it is useful.

from ish import Condition, Workflow

wf = (Workflow(workflow_id="review-42", entry="start")
.add_step("start", "load")
.add_step("left", "left")
.add_step("right", "right")
.add_join_step("join", "join")
.add_map_step("items", "inspect", "items", "reports", max_concurrency=4)
.add_step("draft", "draft")
.add_step("verify", "verify")
.add_parallel("start", "left", "right")
.add_edge("left", "join").add_edge("right", "join")
.add_edge("join", "items").add_edge("items", "draft")
.add_edge("draft", "verify")
.add_conditional("verify", [{"when": Condition.lt("score", 0.8), "to": "draft"}], "END")
.set_reducer("reports", "append").set_reducer("score", "max")
.max_steps(20))
result = await wf.run(initial_state, handler=run_named_step)

An Agent is a separate primitive for autonomous model/tool loops. It does not force agent behavior into a fixed graph. Choose DurableWorkflow when application code must define the control flow.

The snippets show the builder shape. initial_state, branches, and runNamedStep are application values: define the initial JSON state, condition branches, and handler registry before calling run. You can also register a callback on each step when a shared handler registry is not useful.

One workflow, from step replay to control flow​

The builders below create the same workflow shape. The parallel branch outputs are merged in declaration order, map output preserves item order, and the condition loops through draft until the score reaches the threshold or max_steps stops execution.

Conditions, joins, bounded execution​

Conditions are JSON predicates, not host callbacks. Supported operators are eq, ne, gt, gte, lt, lte, in, not_in, contains, starts_with, exists, is_empty, all, any, and not. A conditional's branches are checked in order, then its default edge is followed. Join reducers include append, sum, and merge_object; Rust merges parallel results in declared edge order. Map steps read an array at items_path, limit active callbacks to max_concurrency, and store results at output_key in input order.

Use max_steps and per-step max_visits to stop loops that run too long. A step can require approval or name a cleanup handler. If the workflow fails, compensate_on_failure runs configured cleanup handlers in reverse completion order, including successful sibling branches. If cleanup also fails, the error reports both the original failure and cleanup failures. Cleanup is best effort; use request IDs or status checks to make retries of external actions safe.

Persistence, connectors, replay​

DurableWorkflow accepts a SQLite path, an in-memory store, or an existing PersistenceStore / ConnectorPersistence. When a caller passes a store, closing the workflow does not close that caller-owned store. Reusing the same workflow ID and store reuses completed step results without calling their handlers again. StorageConnector lets an application keep those records in PostgreSQL, SQL Server, Redis, or another database it operates. Its conditional update must be one atomic database operation so two workers cannot claim the same work. See Custom Storage Connectors.

store = ConnectorPersistence(my_connector)
workflow = Workflow(workflow_id="review-42", persistence=store, entry="load")

Keep callbacks limited to a step's work. They do not choose branches, evaluate predicates, reduce state, or decide whether a completed step should replay.