0
0
LangChainframework~5 mins

Human-in-the-loop with LangGraph in LangChain

Choose your learning style9 modes available
Introduction

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.

When a computer program is unsure about an answer and needs a person to check.
When you want to improve AI results by adding human feedback.
When automating tasks but want a person to review important steps.
When building chatbots that ask humans for help on tricky questions.
When combining AI and human work to get better accuracy.
Syntax
LangChain
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.

Examples
This example shows a simple AI step followed by a human review step.
LangChain
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')
Shows what happens if you try to run a node that does not exist (usually an error or no output).
LangChain
graph = LangGraph()
# Empty graph run
result = graph.run('nonexistent_node')
Graph with only one human node, no AI steps.
LangChain
graph = LangGraph()
graph.add_node('only_human', lambda: HumanMessage(content='Human input only'))
result = graph.run('only_human')
Graph with AI start and human final check.
LangChain
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')
Sample Program

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.

LangChain
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()
OutputSuccess
Important Notes

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.

Summary

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.