Skip to content

ValidationException: User messages cannot contain tool uses when repairing orphaned tool_use blocks #2214

@skoolzebra

Description

@skoolzebra

Repro Steps

  1. Start a long-running conversation (80+ messages over multiple days)
  2. Trigger specialist tool timeouts or interruptions that leave orphaned tool_use blocks in conversation history
  3. Allow conversation to grow until context window trimming is needed
  4. Context trimming fails repeatedly (24+ attempts) due to inability to find valid trim points with orphaned blocks
  5. On next user message, RepositorySessionManager.initialize() calls _fix_broken_tool_use()
  6. The repair logic adds synthetic toolResult blocks but incorrectly assigns tool_use blocks to user role messages
  7. Bedrock Converse API rejects the request with ValidationException

Expected Behavior

When _fix_broken_tool_use() repairs orphaned tool_use blocks by adding synthetic toolResult blocks, it should ensure tool_use blocks remain in assistant messages only (per AWS Bedrock API specification).

Actual Behavior

The repair logic creates malformed message history where tool_use blocks appear in user role messages, causing AWS Bedrock to reject the request with:

An error occurred (ValidationException) when calling the Converse operation: User messages cannot contain tool uses. Please remove the tool uses and try again.

Additional Context

  • Bug introduced in PR fix: Fix broken converstaion with orphaned toolUse #1123 (commit 417ebea), first shipped in v1.15.0 (2025-11-04)
  • Observed in production on 2026-04-27 in a conversation that accumulated 80 messages over 5 days
  • Preceded by log message: Session message history has an orphaned toolUse with no toolResult. Adding toolResult content blocks to create valid conversation
  • Root cause: Orphaned tool blocks + context window overflow + failed trimming created the conditions for this bug
  • Only 1 conversation affected out of entire system over a 7-day period (isolated but reproducible)
  • AWS Bedrock API specification requires tool_use blocks ONLY in assistant messages, never user messages

Logs Showing the Issue

2026-04-27T17:13:59.958095Z Session message history has an orphaned toolUse with no toolResult. Adding toolResult content blocks to create valid conversation.
2026-04-27T17:14:01Z botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: User messages cannot contain tool uses. Please remove the tool uses and try again.

Possible Solution

Modify _fix_broken_tool_use() in src/strands/session/repository_session_manager.py to ensure that when adding synthetic toolResult blocks, tool_use blocks remain assigned to assistant role messages. Specifically:

  1. When detecting orphaned tool_use blocks, verify they are in assistant messages
  2. When adding toolResult blocks, add them to the same assistant message that contains the tool_use
  3. Never create or modify user messages to contain tool_use blocks
  4. Add validation before returning from _fix_broken_tool_use() to assert no user messages contain tool_use blocks

Alternative simpler fix: Instead of adding synthetic toolResult blocks, strip orphaned tool_use blocks entirely from message history (trade-off: loses incomplete tool invocation context).

Related

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