Skip to content

fix(bedrock): reorder reasoning blocks first in assistant messages for multi-turn thinking#1848

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/thinking-block-reorder-1698
Open

fix(bedrock): reorder reasoning blocks first in assistant messages for multi-turn thinking#1848
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/thinking-block-reorder-1698

Conversation

@giulio-leone
Copy link

Problem

When extended thinking is enabled and a session manager persists/reloads conversation history, assistant messages may have reasoningContent blocks in wrong order. Bedrock strictly requires that if an assistant message contains any thinking blocks, the first block must be thinking:

botocore.errorfactory.ValidationException: If an assistant message contains
any thinking blocks, the first block must be thinking. Found text

This breaks multi-turn conversations when session managers serialize and deserialize messages without preserving block order.

Root Cause

_format_bedrock_messages() formats and filters content blocks but does not enforce Bedrock's ordering constraint. When messages come from external session managers (e.g., AgentCoreMemorySessionManager), blocks may arrive in arbitrary order.

Fix

Added defensive reordering in _format_bedrock_messages(): after formatting each assistant message's content blocks, check if reasoning blocks exist but are not at the beginning. If so, move all reasoningContent blocks before other blocks.

The reordering only triggers when needed (reasoning blocks exist but aren't first), so there's zero overhead for non-thinking conversations.

Tests

  • Added test_format_bedrock_messages_reorders_reasoning_blocks_first that verifies blocks are correctly reordered when reasoningContent appears after text in an assistant message
  • All 124 existing Bedrock tests pass

Fixes #1698

@giulio-leone
Copy link
Author

Friendly ping — CI is green, tests pass, ready for review whenever convenient. Happy to address any feedback. Thanks! 🙏

…r multi-turn thinking

Bedrock requires reasoningContent blocks to precede all other content blocks
in assistant messages when thinking is enabled. Session managers or message
reconstruction can produce blocks in wrong order, causing ValidationException:
'If an assistant message contains any thinking blocks, the first block must
be thinking.'

Defensively reorder content blocks in _format_bedrock_messages() so reasoning
always comes first in assistant messages, regardless of storage order.

Fixes strands-agents#1698
@giulio-leone giulio-leone force-pushed the fix/thinking-block-reorder-1698 branch from 237fcf7 to 62e2cfc Compare March 9, 2026 20:50
@github-actions github-actions bot added size/s and removed size/s labels Mar 9, 2026
@giulio-leone
Copy link
Author

Friendly ping — this branch has been refreshed on the latest upstream base and all current review feedback has been addressed. It should be ready for review whenever you have a chance. Happy to make any further changes quickly.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Extended thinking breaks multi-turn sessions: SDK-constructed assistant messages lack reasoningContent blocks

1 participant