Skip to content

[BUG] Regression: ValidationException due to orphaned tool_use blocks after removing message processor #495

@cagataycali

Description

@cagataycali

Description

After PR #417 (refactor: Remove message processor), we're experiencing validation errors when tool results are not completed by Bedrock and the agent receives a message end event, followed by a customer asking another question.

Root Cause

The removal of clean_orphaned_empty_tool_uses(agent.messages) from the event loop cycle has eliminated the cleanup mechanism for orphaned empty tool uses. This function was responsible for handling cases where Bedrock doesn't complete tool results.

Error Details

❌ Error: An error occurred (ValidationException) when calling the ConverseStream operation: The model returned the following 
errors: messages.8: `tool_use` ids were found without `tool_result` blocks immediately after: tooluse_p5c8yrxwSXOO-FOx6CBC_A. 
Each `tool_use` block must have a corresponding `tool_result` block in the next message.

Steps to Reproduce

  1. Start an agent conversation
  2. Have Bedrock not complete tool results (this happens often)
  3. Agent receives message end event from Bedrock
  4. Customer asks another question
  5. ValidationException occurs due to orphaned tool_use blocks

Expected Behavior

Orphaned empty tool uses should be cleaned up automatically to prevent validation errors, allowing the conversation to continue smoothly.

Actual Behavior

ValidationException is thrown, breaking the conversation flow.

Proposed Solution

Reintroduce the clean_orphaned_empty_tool_uses function or implement an equivalent mechanism to handle orphaned tool_use blocks that don't have corresponding tool_result blocks.

Related PR

Impact

  • Severity: High - Breaks conversation flow
  • Frequency: Often (happens when Bedrock doesn't complete tool results)
  • User Experience: Poor - Users encounter validation errors mid-conversation

Additional Context

This is a regression introduced by the message processor removal. The cleanup mechanism was essential for handling Bedrock's inconsistent tool result completion behavior.

Sub-issues

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions