[PR #4900] Fix: Resolve duplicate tool results in human approval flow #4397 #4128

Closed
opened 2026-02-20 17:49:42 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/4900

State: closed
Merged: No


Description

This PR fixes a critical bug where tools requiring human approval were generating multiple ToolMessage objects for single tool calls, causing "Too many tool_result blocks found" errors in the human approval workflow.

Problem

The existing implementation had several issues:

  • Flawed tool_result_handler logic with premature break statements
  • Graph flow problems causing repeated tool executions
  • Manual message manipulation that violated LangGraph design patterns
  • Missing duplicate prevention mechanisms

Solution

Root Cause Analysis

  • Tool Result Handler: The handler was not properly checking existing tool call IDs before processing
  • Graph Architecture: Manual message manipulation was bypassing LangGraph's built-in state management
  • Execution Flow: Missing safeguards allowed duplicate tool executions

Changes Made

  1. Fixed tool result handler logic - Properly validate existing tool call IDs before processing
  2. Added duplicate prevention - Implement checks in tool execution nodes to prevent multiple executions
  3. Simplified graph architecture - Removed problematic manual result handler
  4. Leveraged built-in functionality - Use add_messages for automatic deduplication following LangGraph patterns
  5. Comprehensive test coverage - Added test suite to reproduce and verify the fix

Testing

Test Files Added

  • libs/langgraph/tests/test_human_approval_tool_duplication_fix.py - Comprehensive test reproducing the bug and verifying fix
  • libs/langgraph/tests/test_human_approval_simple_fix.py - Simplified test case for basic validation
  • examples/human_in_the_loop/fixed_human_approval_example.py - Working example demonstrating the fix

Test Coverage

  • Reproduces original "Too many tool_result blocks found" error
  • Verifies single tool execution per approval
  • Tests backwards compatibility
  • Validates proper state management

Documentation

Documentation Added

  • docs/human_approval_tool_duplication_solution.md - Detailed technical solution documentation
  • SOLUTION_SUMMARY.md - High-level summary of changes and rationale

Backwards Compatibility

This change maintains full backwards compatibility. Existing code using the human approval workflow will continue to work without modifications while benefiting from the duplicate prevention fixes.

Checklist

  • Follows LangGraph design principles for state management
  • Maintains backwards compatibility
  • Includes comprehensive test coverage
  • Adds appropriate documentation
  • Fixes critical bug without breaking changes
  • Uses built-in LangGraph patterns (add_messages) instead of manual manipulation

Related Issues

Fixes #4397

Additional Notes

The solution follows LangGraph's core philosophy of leveraging built-in state management rather than manual message manipulation. This approach not only fixes the immediate duplication bug but also makes the code more maintainable and aligned with framework best practices.

**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/4900 **State:** closed **Merged:** No --- ## Description This PR fixes a critical bug where tools requiring human approval were generating multiple `ToolMessage` objects for single tool calls, causing "Too many tool_result blocks found" errors in the human approval workflow. ## Problem The existing implementation had several issues: - Flawed `tool_result_handler` logic with premature break statements - Graph flow problems causing repeated tool executions - Manual message manipulation that violated LangGraph design patterns - Missing duplicate prevention mechanisms ## Solution ### Root Cause Analysis - **Tool Result Handler**: The handler was not properly checking existing tool call IDs before processing - **Graph Architecture**: Manual message manipulation was bypassing LangGraph's built-in state management - **Execution Flow**: Missing safeguards allowed duplicate tool executions ### Changes Made 1. **Fixed tool result handler logic** - Properly validate existing tool call IDs before processing 2. **Added duplicate prevention** - Implement checks in tool execution nodes to prevent multiple executions 3. **Simplified graph architecture** - Removed problematic manual result handler 4. **Leveraged built-in functionality** - Use `add_messages` for automatic deduplication following LangGraph patterns 5. **Comprehensive test coverage** - Added test suite to reproduce and verify the fix ## Testing ### Test Files Added - `libs/langgraph/tests/test_human_approval_tool_duplication_fix.py` - Comprehensive test reproducing the bug and verifying fix - `libs/langgraph/tests/test_human_approval_simple_fix.py` - Simplified test case for basic validation - `examples/human_in_the_loop/fixed_human_approval_example.py` - Working example demonstrating the fix ### Test Coverage - Reproduces original "Too many tool_result blocks found" error - Verifies single tool execution per approval - Tests backwards compatibility - Validates proper state management ## Documentation ### Documentation Added - `docs/human_approval_tool_duplication_solution.md` - Detailed technical solution documentation - `SOLUTION_SUMMARY.md` - High-level summary of changes and rationale ## Backwards Compatibility ✅ This change maintains full backwards compatibility. Existing code using the human approval workflow will continue to work without modifications while benefiting from the duplicate prevention fixes. ## Checklist - [x] Follows LangGraph design principles for state management - [x] Maintains backwards compatibility - [x] Includes comprehensive test coverage - [x] Adds appropriate documentation - [x] Fixes critical bug without breaking changes - [x] Uses built-in LangGraph patterns (`add_messages`) instead of manual manipulation ## Related Issues Fixes #4397 ## Additional Notes The solution follows LangGraph's core philosophy of leveraging built-in state management rather than manual message manipulation. This approach not only fixes the immediate duplication bug but also makes the code more maintainable and aligned with framework best practices.
yindo added the pull-request label 2026-02-20 17:49:42 -05:00
yindo closed this issue 2026-02-20 17:49:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#4128