LangGraph Human-in-the-loop Demo
This project demonstrates how to create a LangGraph that pauses execution to ask the user for confirmation via a custom interrupt. The user is prompted in the console using questionary, and the graph resumes once the user provides an answer.
Features
- Custom interrupt node: Triggers a pause and sends a structured payload (type, question, options).
- Human-in-the-loop: The graph waits for user input before continuing.
- State persistence: Uses an in-memory checkpoint to resume execution after the interrupt.
- Simple console UI: Uses
questionaryfor interactive prompts.
Requirements
- Python 3.10 or newer
langgraphquestionary
Install the dependencies with:
pip install -r requirements.txt
Running the Demo
python src/main.py
You will see a prompt:
Уверены, что хотите продолжить?
❯ approve
❯ reject
Select an option. After you choose, the script will print the final state, which includes the user's answer.
Project Structure
├── src/
│ └── main.py # Main script with graph definition and run loop
├── requirements.txt # Python dependencies
└── README.md # This file
How It Works
-
Graph Definition
The graph has a single nodeask_user.- On first run, it triggers an interrupt with a payload containing a question and options.
- After the user responds, the node receives the answer via the
resumeparameter and stores it in the state.
-
Interrupt Handling
The main loop listens for chunks fromgraph.stream().- When an interrupt chunk is detected (
"__interrupt__"key), it displays the question usingquestionary.select. - The chosen answer is sent back to the graph via
Command(resume=answer).
- When an interrupt chunk is detected (
-
Resuming Execution
The graph resumes from the same node, now with the answer available in the state.
The final state is printed to the console.
Feel free to extend the graph with more nodes or integrate it with other LangChain components.
Enjoy experimenting with LangGraph and human-in-the-loop workflows!