Skip to content

Conversation

@zhangzhefang-github
Copy link

@zhangzhefang-github zhangzhefang-github commented Nov 3, 2025

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_calls in the AIMessage, 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_calls attribute (last_ai_msg.tool_calls = revised_tool_calls), but LangGraph's state management doesn't properly persist direct object mutations.

The Fix:

  • Create a new AIMessage instance instead of mutating the original
  • Ensure the message has an ID (generate UUID if needed) so LangGraph's add_messages reducer properly replaces the message instead of appending a duplicate
  • Copy all relevant fields (content, name, metadata, etc.) to the new message

Testing:

  • Added comprehensive test case test_human_in_the_loop_middleware_edit_actually_executes_with_edited_args that simulates the exact scenario from the issues
  • Verifies the edited tool call executes with correct arguments
  • Verifies the AIMessage in state contains edited (not original) parameters
  • Confirms no duplicate executions occur
  • All 16 existing HITL middleware tests pass

Issue

Fixes #33787
Fixes #33784

Dependencies

No new dependencies added.

@github-actions github-actions bot added fix langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0 and removed fix labels Nov 3, 2025
@zhangzhefang-github zhangzhefang-github force-pushed the fix/hitl-edit-persistence branch 2 times, most recently from 1c13e07 to 5bd39be Compare November 3, 2025 02:01
@zhangzhefang-github zhangzhefang-github changed the title fix(agents): ensure HITL middleware edits persist correctly fix(langchain_v1): ensure HITL middleware edits persist correctly Nov 3, 2025
@github-actions github-actions bot added the fix label Nov 3, 2025
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
@zhangzhefang-github
Copy link
Author

@sydney-runkle Hi! I've investigated the CI failure and found:

The failing test is unrelated to my PR:

  • My PR only modifies libs/langchain_v1 code (HITL middleware)
  • The failing test is in libs/core/tests/unit_tests/runnables/test_runnable.py
  • I didn't modify any libs/core files

The test only fails on Python 3.12:

  • ✅ Passes on Python 3.10 (both master and this PR branch)
  • ❌ Fails on Python 3.12 (CI environment)
  • Error: ValueError('generator already executing')

All my HITL tests pass:

This appears to be a Python 3.12-specific issue in libs/core, possibly related to recent tracing changes (commit 76dd656). Could you please re-run CI or advise how to proceed?

@zhangzhefang-github
Copy link
Author

Hi @sydney-runkle,

I've reverted the master merge that was causing CI failures. Here's what happened:

Timeline:

  • Nov 3: Original commit (69d4f40) - ✅ All tests passed
  • Nov 4: GitHub suggested updating the branch, so I merged master
  • Result: ❌ CI failed on libs/core tests (unrelated to my changes)

Analysis:

  • The failing test test_runnable_lambda_context_config is in libs/core
  • My PR only modifies libs/langchain_v1 files
  • The test failure is Python 3.12-specific, likely from recent master changes
  • Error: ValueError('generator already executing')

Resolution:

The PR is ready for review. I can merge master again after the core test issue is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0

Projects

None yet

2 participants