Skip to content

fix: ensure assistant messages have reasoningContent when thinking is enabled#1889

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/bedrock-thinking-multi-turn
Open

fix: ensure assistant messages have reasoningContent when thinking is enabled#1889
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/bedrock-thinking-multi-turn

Conversation

@giulio-leone
Copy link
Contributor

Issue

Closes #1698

Problem

When extended thinking is enabled, Bedrock requires every assistant message to start with a reasoningContent block. Custom session managers (e.g., AgentCoreMemorySessionManager) or external history providers may strip these blocks during serialization/deserialization, causing a ValidationException on subsequent API calls:

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

Root Cause

The SDK correctly constructs reasoningContent blocks during streaming and preserves them through the event loop. However, custom session managers that serialize/deserialize conversation history may strip reasoningContent blocks, breaking Bedrock's multi-turn thinking constraint.

Solution

Added defensive handling in _format_bedrock_messages() to ensure assistant messages conform to Bedrock's thinking requirements:

  1. Missing reasoningContent: When thinking is enabled and an assistant message has no reasoningContent block, injects a minimal redactedContent placeholder at position 0
  2. Misordered blocks: When reasoningContent exists but is not the first content block, reorders blocks to put reasoning first

This is a defensive measure — it handles misbehaving session managers without breaking correct flows where reasoningContent is already properly preserved.

Testing

Added 6 new tests covering:

  • test_format_request_injects_reasoning_when_thinking_enabled_and_missing — placeholder injection when thinking blocks are stripped
  • test_format_request_no_injection_when_thinking_disabled — no modification without thinking
  • test_format_request_preserves_existing_reasoning_when_thinking_enabled — existing blocks preserved
  • test_format_request_reorders_reasoning_to_first_when_thinking_enabled — block reordering
  • test_format_request_multi_turn_thinking_session_reload — multi-turn session reload scenario
  • test_format_request_does_not_inject_reasoning_for_user_messages — user messages untouched

All 131 tests pass (125 existing + 6 new).

… enabled

When extended thinking is enabled, Bedrock requires every assistant message
to start with a reasoningContent block. Custom session managers (e.g.,
AgentCoreMemorySessionManager) or external history providers may strip
these blocks during serialization, causing ValidationException on
subsequent API calls.

This fix adds defensive handling in _format_bedrock_messages():
- Injects a minimal redactedContent placeholder when reasoningContent is
  missing from assistant messages with thinking enabled
- Reorders content blocks to ensure reasoningContent comes first when it
  exists but is not at position 0

Closes strands-agents#1698
@giulio-leone giulio-leone force-pushed the fix/bedrock-thinking-multi-turn branch from 6b1094e to 853244c Compare March 15, 2026 16:12
@github-actions github-actions bot added size/m and removed size/m labels Mar 15, 2026
@giulio-leone
Copy link
Contributor Author

Friendly ping — ensures assistant messages include reasoningContent when thinking mode is enabled, preventing missing reasoning context in multi-turn conversations.

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