Skip to content

AgentCore Memory + Browser Tool: Conversation history corruption causing orphaned toolUse #245

Open

Description

@very99

Bug Description

When using AgentCoreMemorySessionManager with AgentCoreBrowser (Browser Tool) in Strands Agents, the conversation history becomes corrupted after the first message, leading to ValidationException errors.

Environment

  • bedrock-agentcore: Latest version
  • Strands Agents: Latest version
  • strands-agents-tools: Latest version
  • Region: us-east-1
  • Memory ID: memory_agentcore_demo_complex-MOvGs2AZQ5

Problem Description

Error Message

Session message history has an orphaned toolUse with no toolResult. 
Adding toolResult content blocks to create valid conversation.

ValidationException: The number of toolResult blocks at messages.27.content 
exceeds the number of toolUse blocks of previous turn.

Behavior

  • ✅ First message in session: Works correctly
  • ❌ Subsequent messages in same session: Fails with ValidationException
  • ✅ Without Browser Tool: Memory works correctly with other tools (Gateway tools)
  • ❌ With Browser Tool: Memory appears to save corrupted conversation history

Code Example

from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
from strands import Agent
from strands_tools.browser import AgentCoreBrowser

# Memory configuration
memory_config = AgentCoreMemoryConfig(
    memory_id="memory_agentcore_demo_complex-MOvGs2AZQ5",
    session_id="test_session_123",
    actor_id="test_user"
)

session_manager = AgentCoreMemorySessionManager(
    agentcore_memory_config=memory_config,
    region_name="us-east-1"
)

# Browser Tool
browser_tool = AgentCoreBrowser(region="us-east-1")

# Agent with Browser Tool + Memory
agent = Agent(
    model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
    system_prompt="You are a helpful assistant.",
    session_manager=session_manager,
    tools=[browser_tool.browser]
)

# First call: SUCCESS ✅
result1 = agent("Hello")

# Second call in same session: FAILS ❌
# Memory loads corrupted history with orphaned toolUse
result2 = agent("How are you?")

Root Cause Hypothesis

The issue appears to be in how AgentCoreMemorySessionManager saves and loads conversation history when Browser Tool is involved:

Possible Causes:

  1. Incomplete tool call persistence:

    • Browser Tool creates tool_use blocks
    • Memory saves the conversation
    • On reload, tool_use blocks are missing or corrupted
    • But tool_result blocks are preserved
    • This creates "orphaned toolUse"
  2. Serialization/Deserialization issue:

    • Browser Tool's conversation format may not be fully compatible with Memory's storage format
    • Some data is lost during save/load cycle
  3. Context truncation without cleanup:

    • Memory may truncate conversation history
    • But doesn't properly clean up orphaned tool blocks

Logs

2026-02-02T10:12:24.909000+00:00 Session message history has an orphaned toolUse with no toolResult. Adding toolResult content blocks to create valid conversation.

2026-02-02T10:12:25.876000+00:00 botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ConverseStream operation: The number of toolResult blocks at messages.27.content exceeds the number of toolUse blocks of previous turn.

Testing Results

Configuration Result
Memory + Gateway Tools ✅ Works
Memory + Browser Tool ❌ Fails after first message
No Memory + Browser Tool ❓ Not tested (Memory required)
Memory + Gateway + Browser ❌ Fails after first message

This strongly suggests the issue is in how Memory handles Browser Tool's conversation history.

Questions

  1. Is Browser Tool + Memory a tested and supported combination?
  2. Are there known limitations or special handling required for Browser Tool with Memory?
  3. Does Memory have special serialization logic for different tool types?
  4. Is there documentation on conversation history format requirements?

Expected Behavior

AgentCore Memory should correctly save and restore conversation history when Browser Tool is used, maintaining proper tool_use/tool_result pairing.

Actual Behavior

After the first message, Memory loads corrupted conversation history with orphaned tool blocks, causing Bedrock API to reject subsequent requests.

Related Issues

Suggested Investigation

  1. Add logging to show exactly what Memory saves vs. what it loads
  2. Compare conversation history format for Browser Tool vs. other tools
  3. Check if Browser Tool's message format requires special handling
  4. Verify that all tool blocks are properly paired during save/load

Suggested Fix

Add validation and cleanup in AgentCoreMemorySessionManager:

  1. Before saving: Validate that all tool_use blocks have corresponding tool_result blocks
  2. After loading: Sanitize conversation history to remove orphaned tool blocks
  3. Add specific handling for Browser Tool if its format differs from standard tools

Impact

This bug prevents using Browser Tool with Memory, which is a critical limitation for building context-aware agents that need web browsing capabilities.

Activity

  1. very99 commented on Feb 2, 2026

    @very99
    Author

    🔍 Additional Research: This is a Widespread Known Issue

    After further investigation, I found that this "orphaned toolUse/toolResult" error is a widespread known issue affecting multiple projects and frameworks, not just AgentCore Memory.

    📋 Related Issues Across Multiple Projects

    1. Anthropic Claude Code #14416 - "Context compaction breaks tool_use/tool_result pairing"

    2. Anthropic Claude Code #14173 - "orphaned tool_result in messages array despite valid conversation history"

    3. Anthropic Claude Code #12894 - Related bug report

      • Referenced by multiple developers as a known issue
    4. Pydantic AI #2327 - "Bedrock Agent Error: The number of toolResult blocks exceeds..."

    5. OpenHands #4912 - "BedrockException: The number of toolResult blocks at messages.2.content exceeds..."

    6. LiteLLM #7099 - "The wrong number of toolUse blocks when using AWS Bedrock"

    🎯 Root Cause (from Anthropic Issue #14416)

    "During automatic context compaction in long sessions, Claude Code sometimes removes a tool_use block while keeping its corresponding tool_result block. This breaks the required API pairing and causes the conversation to become corrupted."

    💡 Why AgentCore Memory + Browser Tool Triggers This

    The combination of AgentCore Memory + Browser Tool likely triggers this issue because:

    1. Browser Tool generates many tool calls (navigation, clicking, reading, extracting data, etc.)
    2. AgentCore Memory persists the conversation history across requests
    3. As the session grows longer (note: error at messages.27.content), some context compaction/truncation mechanism is triggered
    4. During compaction/loading, tool_use blocks are removed or corrupted but tool_result blocks remain
    5. This creates orphaned tool results, causing the ValidationException

    This matches the pattern described in Anthropic Issue #14416 exactly.

    📚 Community Workarounds

    From https://calvin.my/posts/api-error-claude-block-must-have-a-corresponding-tool_use-block-in-the-previous-message:

    Temporary workarounds include:

    1. End the current conversation and start a new one (use new session_id)
    2. Use /compact command to wipe conversation history while maintaining summarized context
    3. Use /rewind command to go back 1-2 steps

    ✅ Conclusion

    This is not an AgentCore Memory-specific issue but rather a fundamental problem with context management in tool-using conversations that affects:

    • Anthropic Claude Code
    • Pydantic AI
    • OpenHands
    • LiteLLM
    • AgentCore Memory (this issue)

    The issue needs to be addressed in AgentCoreMemorySessionManager to properly handle tool_use/tool_result pairing during:

    • Conversation history save/load
    • Context compaction/truncation
    • Message serialization/deserialization

    🔧 Suggested Fix (from community)

    Add a sanitization function in AgentCoreMemorySessionManager that:

    1. Before saving to Memory: Validate that all tool_use blocks have corresponding tool_result blocks
    2. After loading from Memory: Sanitize conversation history by:
      • Collecting all tool_use_id values from tool_use blocks
      • Removing any tool_result blocks whose tool_use_id doesn't have a matching tool_use block
    3. Run this sanitization before passing conversation history to Strands Agent

    This should prevent orphaned tool blocks from reaching the Bedrock API.

    🔗 Cross-Reference

    Also reported in Strands Agents: strands-agents/harness-sdk#1610

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions