Subgraphs
A compiled graph can be a node of another graph. That keeps a large workflow in parts that are built and tested on their own, each with its own state class. Two functions connect the states: state reads the state of the subgraph out of the state of the graph around it, and update writes it back.
@Serializable
data class ReturnCase(
val customer: String,
val items: List<Int>,
val payout: Payout? = null, // the state of the subgraph; null until the run enters it
val reply: String = "",
)
// payoutGraph is the graph of "Ask from inside a node": a CompiledGraph<Payout> that asks before a large payout.
val graph = StateGraph<ReturnCase> {
val check = node("check") { case -> case.copy(items = case.items.filter { it > 0 }) }
val payout = subgraph(
"payout", payoutGraph,
// The state the subgraph works on: the saved one, or a new one on the first visit.
state = { case -> case.payout ?: Payout(case.customer, case.items) },
// Called when the subgraph finishes, and when it pauses.
update = { case, payout -> case.copy(payout = payout) },
)
val reply = node("reply") { case -> case.copy(reply = "Dear ${case.customer}: ${case.payout?.log?.last()}.") }
START then check then payout then reply then END
}.compile()
// One config and one checkpointer, for the graph around. The subgraph needs none of its own.
val config = GraphConfig(threadId = "return-7", checkpointer = MemoryCheckpointer<ReturnCase>())
val paused = graph.invoke(ReturnCase("Ben", items = listOf(200, 50)), config)
if (paused is GraphResult.Interrupted) {
val answer = askManager(paused.state.payout?.question) // your UI
// The answer goes into the state of the subgraph, and the run continues inside it.
graph.resume(config) { it.copy(payout = it.payout?.copy(approved = answer)) }
}Runnable version: Subgraph.
The subgraph runs from its
STARTto itsENDinside one step of the graph around it. Its node can run next to other nodes without a reducer, like a node with aworkand anupdate.When a node of the subgraph calls
interrupt, the graph around pauses too. The checkpoint remembers where the subgraph stands, soresumecontinues at the node that paused, and the nodes of the subgraph that came before it do not run again.For that,
statehas to return whatupdatestored. Keep the whole state of the subgraph in a property of the outer state, aspayoutabove. A subgraph that never pauses can get a new state on every visit and give back only its result:state = { Payout(it.customer, it.items) }, update = { case, payout -> case.copy(reply = payout.log.last()) }.interruptBeforeandinterruptAfterof theGraphConfigreach the nodes of a subgraph by their path: the name of the subgraph node, a/and the name of the node inside it. The graph around pauses in the step of the subgraph node, andresumecontinues inside the subgraph:kotlin// Pause before the node "pay" of payoutGraph for every payout, not only a large one. val config = GraphConfig("return-7", checkpointer, interruptBefore = setOf("payout/pay"))GraphResult.Interrupted.nextNodesnames the subgraph node (payout), and thesubgraphsof the checkpoint say where inside it the run stands. A path can go deeper, such as"payout/checks/limit"for a node of a subgraph inside the subgraph, and a pause after the last node of a subgraph comes before the graph around goes on. That is why a node name cannot hold a/.A subgraph with the same state class needs no functions:
subgraph("payout", payoutGraph).A stream of the graph around has the events of the subgraph too, each inside a
GraphEvent.SubgraphEventthat names the subgraph's node. They are the events a stream of the subgraph alone would have, with the state of the subgraph:kotlingraph.stream(case, config).collect { event -> if (event is GraphEvent.SubgraphEvent) { val inside = event.event // a GraphEvent of payoutGraph if (inside is GraphEvent.NodeStarted) println("${event.node} > ${inside.node} started") } }For a subgraph inside a subgraph,
event.eventis aSubgraphEventagain;event.pathlists the subgraph nodes from the outside in, andevent.innermostis the event at the end.textDeltareads a model's text from any depth.A failure inside the subgraph is the
causeof theNodeExecutionExceptionof its node.maxIterationslimits the steps of the subgraph on their own.With a checkpointer,
resumeafter a failure continues at the step of the subgraph that failed, and its finished nodes do not run again. The other nodes of the step around it do run again. This takes the same thing as a pause:statehas to read back whatupdatewrote. The engine checks that, so a subgraph that gets a new state on every visit starts again from its first node, as does one that was cancelled because another node of the step failed.A subgraph can contain subgraphs.