Bug Description
When AgentCoreMemorySessionManager restores conversation history that contains orphaned toolUse blocks (toolUse without matching toolResult), the repository_session_manager attempts to fix the history by adding toolResult blocks. However, the fix adds an incorrect number of toolResult blocks, causing Bedrock's ConverseStream API to reject the request with:
ValidationException: The number of toolResult blocks at messages.N.content exceeds the number of toolUse blocks of previous turn.
Steps to Reproduce
- Use
AgentCoreMemorySessionManager with Bedrock AgentCore Memory enabled
- During an agent invocation, have a tool call interrupted (e.g., MCP server timeout, network error) — this leaves a
toolUse block in the assistant message without a corresponding toolResult in the next user message
- Memory saves this incomplete conversation history
- On the next invocation in the same session, Memory restores the history
repository_session_manager detects the orphaned toolUse and logs:
WARNING:strands.session.repository_session_manager:Session message history has an orphaned toolUse with no toolResult. Adding toolResult content blocks to create valid conversation.
- The agent then logs:
INFO:strands.agent.agent:Agents latest message is toolUse, appending a toolResult message to have valid conversation.
- The combined fixes result in more toolResult blocks than toolUse blocks, and Bedrock rejects the request
Expected Behavior
The repair logic should ensure the number of toolResult blocks exactly matches the number of toolUse blocks in the previous assistant message.
Environment
- strands-agents: 1.39.0
- bedrock-agentcore: 1.9.1
- Model: zai.glm-5 via Bedrock ConverseStream API
- Region: us-east-2
Workaround
Manually validate and fix the message history after session restoration, before calling the model.
Bug Description
When
AgentCoreMemorySessionManagerrestores conversation history that contains orphanedtoolUseblocks (toolUse without matching toolResult), therepository_session_managerattempts to fix the history by adding toolResult blocks. However, the fix adds an incorrect number of toolResult blocks, causing Bedrock's ConverseStream API to reject the request with:Steps to Reproduce
AgentCoreMemorySessionManagerwith Bedrock AgentCore Memory enabledtoolUseblock in the assistant message without a correspondingtoolResultin the next user messagerepository_session_managerdetects the orphaned toolUse and logs:Expected Behavior
The repair logic should ensure the number of toolResult blocks exactly matches the number of toolUse blocks in the previous assistant message.
Environment
Workaround
Manually validate and fix the message history after session restoration, before calling the model.