Human-in-the-loop means a person helps a computer program make better decisions. LangGraph lets you build these programs easily by connecting steps and asking for human help when needed.
Human-in-the-loop with LangGraph in LangChain
Start learning this pattern below
Jump into concepts and practice - no test required
from langchain.graphs import LangGraph from langchain.schema import HumanMessage def human_in_loop_example(): graph = LangGraph() # Add nodes (steps) to the graph graph.add_node('step1', lambda: 'AI output') # Add human review node def human_review(input_text): # This simulates asking a human return HumanMessage(content=f'Reviewed: {input_text}') graph.add_node('human_check', human_review) # Connect nodes graph.add_edge('step1', 'human_check') # Run the graph result = graph.run('step1') return result
LangGraph lets you create nodes (steps) and connect them to form a flow.
HumanMessage is used to represent human input or feedback in the flow.
graph = LangGraph() graph.add_node('ai_step', lambda: 'AI says hello') graph.add_node('human_step', lambda x: HumanMessage(content=f'Human checked: {x}')) graph.add_edge('ai_step', 'human_step') result = graph.run('ai_step')
graph = LangGraph() # Empty graph run result = graph.run('nonexistent_node')
graph = LangGraph() graph.add_node('only_human', lambda: HumanMessage(content='Human input only')) result = graph.run('only_human')
graph = LangGraph() graph.add_node('start', lambda: 'Start AI') graph.add_node('end', lambda x: HumanMessage(content=f'Final check: {x}')) graph.add_edge('start', 'end') result = graph.run('start')
This program creates a LangGraph with two steps: an AI step and a human review step. It runs the graph and prints the final human-reviewed message.
from langchain.graphs import LangGraph from langchain.schema import HumanMessage def human_in_loop_demo(): graph = LangGraph() # AI step that generates a message def ai_step(): return 'AI generated text' # Human review step that takes AI output def human_review(input_text): # Simulate human reviewing and confirming return HumanMessage(content=f'Human reviewed: {input_text}') # Add nodes graph.add_node('ai_step', ai_step) graph.add_node('human_review', human_review) # Connect AI step to human review graph.add_edge('ai_step', 'human_review') # Run the graph starting from AI step print('Before running graph:') print('Nodes:', list(graph.nodes.keys())) result = graph.run('ai_step') print('\nAfter running graph:') print('Result:', result.content) if __name__ == "__main__": human_in_loop_demo()
The time complexity depends on the number of nodes and edges in the graph.
Space complexity depends on how much data each node stores and passes.
A common mistake is forgetting to connect nodes, so the flow does not run as expected.
Use human-in-the-loop when AI alone is not reliable enough or needs human judgment.
Human-in-the-loop means combining AI steps with human feedback in a flow.
LangGraph helps build these flows by connecting AI and human nodes.
This approach improves results by letting humans check or fix AI outputs.
Practice
Solution
Step 1: Understand Human-in-the-loop concept
Human-in-the-loop means AI and humans work together, where humans check or improve AI outputs.Step 2: Role of LangGraph in this context
LangGraph helps build flows that connect AI steps with human feedback nodes to improve results.Final Answer:
To combine AI processing steps with human feedback for better results -> Option AQuick Check:
Human-in-the-loop = AI + human feedback [OK]
- Thinking it removes human input
- Assuming it only automates AI without feedback
- Confusing it with fully automated AI pipelines
Solution
Step 1: Recall LangGraph syntax for adding nodes
LangGraph uses flow.add_node() method to add nodes, including human nodes.Step 2: Identify correct human node creation
HumanNode is the class representing human feedback nodes, so flow.add_node(HumanNode(name='review')) is correct.Final Answer:
flow.add_node(HumanNode(name='review')) -> Option CQuick Check:
Use add_node with HumanNode class [OK]
- Using non-existent methods like add_human or insert_human_node
- Forgetting to instantiate HumanNode class
- Passing string directly without node wrapper
flow.add_node(AINode(name='generate'))
flow.add_node(HumanNode(name='check'))
flow.connect('generate', 'check')
result = flow.run(input='Hello')
What will happen when flow.run is called?Solution
Step 1: Analyze flow node order and connections
The AI node 'generate' runs first, then its output is passed to the human node 'check' via connect.Step 2: Understand human node behavior in flow.run
HumanNode pauses for human feedback before continuing, so the flow waits for human input after AI output.Final Answer:
The AI node generates output, then the human node requests feedback before continuing -> Option DQuick Check:
AI runs first, then human feedback [OK]
- Assuming human nodes are skipped automatically
- Thinking human nodes run before AI nodes
- Believing human nodes cause errors when connected
flow.add_node(HumanNode('review'))
flow.connect('review', 'generate')
But it throws an error. What is the likely cause?Solution
Step 1: Check HumanNode instantiation syntax
HumanNode requires named argument 'name', so HumanNode('review') is invalid syntax.Step 2: Confirm connection method accepts node names as strings
Connecting nodes by their names as strings is valid, so error is not from connect method usage.Final Answer:
HumanNode must be instantiated with a named argument like name='review' -> Option AQuick Check:
HumanNode needs name= argument [OK]
- Passing positional argument instead of named argument
- Assuming connect only accepts node objects
- Thinking human nodes cannot be connected
Solution
Step 1: Identify correct node order for the flow
The flow should be AI generate -> human review/edit -> AI summarize final text.Step 2: Check connections match the desired order
flow.add_node(AINode(name='generate')) flow.add_node(HumanNode(name='review')) flow.add_node(AINode(name='summarize')) flow.connect('generate', 'review') flow.connect('review', 'summarize') connects 'generate' to 'review', then 'review' to 'summarize', matching the required sequence.Final Answer:
AI generate, then human review, then AI summarize with correct connections -> Option BQuick Check:
Correct node order and connections = flow.add_node(AINode(name='generate')) flow.add_node(HumanNode(name='review')) flow.add_node(AINode(name='summarize')) flow.connect('generate', 'review') flow.connect('review', 'summarize') [OK]
- Placing human node before AI generate
- Connecting nodes in wrong sequence
- Skipping human review step
