- 
                Notifications
    
You must be signed in to change notification settings  - Fork 19.6k
 
fix(langchain_v1): ensure HITL middleware edits persist correctly #33789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(langchain_v1): ensure HITL middleware edits persist correctly #33789
Conversation
1c13e07    to
    5bd39be      
    Compare
  
    5bd39be    to
    5a2c343      
    Compare
  
    Fix issues langchain-ai#33787 and langchain-ai#33784 where Human-in-the-Loop middleware edits were not persisting correctly in the agent's message history. The problem occurred because the middleware was directly mutating the AIMessage.tool_calls attribute, but LangGraph's state management doesn't properly persist direct object mutations. This caused the agent to see the original (unedited) tool calls in subsequent model invocations, leading to duplicate or incorrect tool executions. Changes: - Create new AIMessage instance instead of mutating the original - Ensure message has an ID (generate UUID if needed) so add_messages reducer properly replaces instead of appending - Add comprehensive test case that reproduces and verifies the fix
5a2c343    to
    69d4f40      
    Compare
  
    | 
           @sydney-runkle Hi! I've investigated the CI failure and found: The failing test is unrelated to my PR: 
 The test only fails on Python 3.12: 
 All my HITL tests pass: 
 This appears to be a Python 3.12-specific issue in   | 
    
e50a698    to
    69d4f40      
    Compare
  
    | 
           Hi @sydney-runkle, I've reverted the master merge that was causing CI failures. Here's what happened: Timeline: 
 Analysis: 
 Resolution: 
 The PR is ready for review. I can merge master again after the core test issue is resolved.  | 
    
Description
Fixes a critical bug in the Human-in-the-Loop middleware where edits to tool calls were not persisting correctly in the agent's message history.
The Problem:
When a user edited a tool call via HITL middleware, the edited tool would execute correctly. However, the agent's subsequent model calls would still see the ORIGINAL (unedited)
tool_callsin theAIMessage, causing the agent to re-attempt the original tool call and leading to duplicate or incorrect executions.Root Cause:
The middleware was directly mutating the
AIMessage.tool_callsattribute (last_ai_msg.tool_calls = revised_tool_calls), but LangGraph's state management doesn't properly persist direct object mutations.The Fix:
AIMessageinstance instead of mutating the originaladd_messagesreducer properly replaces the message instead of appending a duplicateTesting:
test_human_in_the_loop_middleware_edit_actually_executes_with_edited_argsthat simulates the exact scenario from the issuesIssue
Fixes #33787
Fixes #33784
Dependencies
No new dependencies added.