This commit is contained in:
Eugene Yurtsev
2024-12-05 17:16:34 -05:00
parent 4fd261765a
commit 62ff2eb32d
3 changed files with 172 additions and 151 deletions
+3 -90
View File
@@ -453,100 +453,13 @@ Read [this how-to](https://langchain-ai.github.io/langgraph/how-tos/recursion-li
Breakpoints enable **human-in-the-loop** workflows by **pausing** graph execution to allow for human review before continuing.
You **MUST** use a [checkpointer](./persistence.md) when using breakpoints as breakpoints require the ability to save the state of the graph at the time of pausing.
There are two types of breakpoints:
1. **Static breakpoints**: Pause the graph **before** or **after** a node executes.
2. **Dynamic breakpoints**: Pause the graph from **inside** a node.
1. **Static breakpoints**: Pause the graph **before** or **after** a node executes. This is achieved by specifying the `interrupt_before` and `interrupt_after` keys when [compiling your graph](#compiling-your-graph).
2. **Dynamic breakpoints**: Pause the graph from **inside** a node. This is achieved by using the `interrupt` function or raising a `NodeInterrupt` exception.
### Static Breakpoints
Please see the [Human-in-the-Loop guide](../human_in_the_loop) for conceptual information about breakpoints.
To set static breakpoints, specify the `interrupt_before` and/or `interrupt_after` key when [compiling your graph](#compiling-your-graph).
```python
graph = graph_builder.compile(
interrupt_before=["node_a"],
interrupt_after=["node_b", "node_c"],
checkpointer=..., # Required
)
```
When using sub-graphs, specify the `interrupt_before` and `interrupt_after` values when compiling the subgraph.
### Dynamic Breakpoints
There are two ways to interrupt the graph dynamically:
1. `interrupt` **function (recommended)**: Interrupts the graph within a node and surfaces a value to the client as part of the interrupt information.
2. `NodeInterrupt` exception: An older, less flexible method for interrupting.
#### `interrupt`
```python
from langgraph.types import interrupt
def node(state: State):
...
client_value = interrupt(
# This value will be sent to the client.
# It can be any JSON serializable value.
{"key": "value"}
)
...
```
#### `NodeInterrupt`
Throw a `NodeInterrupt` exception to interrupt the graph.
```python
def my_node(state: State) -> State:
if len(state['input']) > 5:
raise NodeInterrupt(f"Received input that is longer than 5 characters: {state['input']}")
return state
```
### Resuming
When a breakpoint is hit, graph execution will pause.
=== "Command"
Resume execution using the new `Command` primitive.
```python
graph.invoke(inputs, config=config) # This will pause at the breakpoint
...
# Do something (e.g., get human input)
...
graph.invoke(
Command(
# Use `resume` to pass a value to the `interrupt`.
resume=resume,
# For other kinds of breakpoints, use `update` to update the state.
update=update,
),
config=config
)
```
=== "Without the Command Primitive"
Resume execution without the `Command` primitive (older versions of LangGraph).
```python
graph.invoke(inputs, config=config) # This will pause at the breakpoint
...
# Do something (e.g., get human input)
...
graph.update_state(update, config=config)
graph.invoke(None, config=config)
```
See [this guide](../how-tos/human_in_the_loop/breakpoints.ipynb) for a full walkthrough of how to add breakpoints.
## Subgraphs