Skip to content

fix(converters): prepend reasoningContent blocks in _openai_to_bedrock()#419

Open
cagataycali wants to merge 1 commit intoaws:mainfrom
cagataycali:fix/reasoning-content-ordering
Open

fix(converters): prepend reasoningContent blocks in _openai_to_bedrock()#419
cagataycali wants to merge 1 commit intoaws:mainfrom
cagataycali:fix/reasoning-content-ordering

Conversation

@cagataycali
Copy link
Copy Markdown
Contributor

Summary

Fixes #416_openai_to_bedrock() appends reasoningContent blocks after text and toolUse, but Bedrock requires thinking blocks to come first.

3-line fix. No new dependencies. No behavioral change for messages without reasoning.

The Bug

# Before: reasoning appended LAST
content_items = [text, toolUse, reasoningContent]  # → ValidationException

# After: reasoning prepended FIRST  
content_items = [reasoningContent, text, toolUse]  # → Correct

Bedrock error:

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

Changes

 content_items: list[dict[str, Any]] = []
+reasoning_items: list[dict[str, Any]] = []
 ...
     for rc in openai_msg.get("_strands_reasoning_content", []):
         if isinstance(rc, dict) and "reasoningContent" in rc:
-            content_items.append(rc)
+            reasoning_items.append(rc)
 ...
-    return {"role": bedrock_role, "content": content_items}
+    # Reasoning blocks MUST come first per Bedrock API:
+    # "If an assistant message contains any thinking blocks, the first block must be thinking."
+    return {"role": bedrock_role, "content": reasoning_items + content_items}

Verification

15 tests across 3 suites all pass:

Suite Tests Result
Strands SDK session managers 5/5 ✅ (no bug here)
AgentCoreMemoryConverter (default) 5/5 ✅ (no bug here)
OpenAIConverseConverter 5/5 ✅ after fix (was 1/5)

Impact

  • Fixes: Multi-turn conversations with extended thinking + OpenAIConverseConverter
  • No impact: Messages without reasoningContent (empty list prepended = no change)
  • No impact: Default AgentCoreMemoryConverter users (already works correctly)

I understand this repo's policy on external PRs — feel free to close if preferred and cherry-pick internally. The fix is also documented in the #416 comment thread.

The _openai_to_bedrock() function appended reasoningContent blocks after
text and toolUse blocks. Bedrock API requires that if an assistant message
contains any thinking blocks, the first block must be a thinking block.

This caused ValidationException on multi-turn conversations with extended
thinking enabled:

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

The fix collects reasoning blocks separately and prepends them to the
content array, matching the original block ordering that Bedrock produced.

Before: [text, toolUse, reasoningContent]  -> ValidationException
After:  [reasoningContent, text, toolUse]  -> Correct

Fixes aws#416
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@640b3ad). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #419   +/-   ##
=======================================
  Coverage        ?   91.24%           
=======================================
  Files           ?       58           
  Lines           ?     4970           
  Branches        ?      756           
=======================================
  Hits            ?     4535           
  Misses          ?      249           
  Partials        ?      186           
Flag Coverage Δ
unittests 91.24% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@phongtran222
Copy link
Copy Markdown

Hi team, I also facing this error for toolUse, can you all help me fix? Appreciate it

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAIConverseConverter produces wrong content block ordering for reasoningContent, breaking Bedrock extended thinking

3 participants