Files
2026-05-26 13:43:26 +00:00

57 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Humanintheloop (interrupt / resume) LangGraph example
## What this project does
This repository contains a minimal, selfcontained example that demonstrates how to use **LangGraph**s custom interrupt mechanism for *Humanintheloop* (HITL). The graph pauses at a node, asks the user for confirmation via an interactive console prompt, and then resumes execution with the chosen answer.
The key concepts shown are:
1. **State definition** a `TypedDict` that holds data produced by the user.
2. **Interrupt node** uses `interrupt(payload)` to pause the graph.
3. **Resume handling** the main loop detects `__interrupt__`, shows a question, collects an answer and sends it back with `Command(resume=payload)`.
4. **Checkpointing** `InMemorySaver` keeps the graph state so that resumption works correctly.
## File structure
| File | Purpose |
|------|---------|
| `requirements.txt` | Python dependencies (langgraph, questionary, pythondotenv) |
| `main.py` | Entry point builds and runs the graph, handles interrupts |
| `README.md` | Project description and usage instructions |
## Installation
```bash
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
# Install dependencies
pip install -r requirements.txt
```
## Running the example
```bash
python main.py
```
You will see a prompt asking whether you want to continue. Choose an option and the graph will resume, printing the final state.
## How it works (highlevel)
1. **Graph construction** two nodes: `ask_user` (interrupt) and `process_answer` (continues after resume).
2. **Interrupt payload** a dictionary with `type`, `question`, and `options`.
3. **Main loop** streams the graph, intercepts `__interrupt__`, uses `questionary.select` to get user input, then resumes the graph with the answer attached.
4. **Final state** after completion, the final state is printed showing the chosen value.
## Extending the example
- Replace the console prompt with a web UI or chatbot interface.
- Add more nodes before/after the interrupt to build richer workflows.
- Persist checkpoints to disk using `FileSaver` if you need longterm state.
---
**Author:** Kirill Kutlakhmetov