Skip to content

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

Merged
jariy17 merged 2 commits intoaws:mainfrom
cagataycali:fix/reasoning-content-ordering
Apr 21, 2026
Merged

fix(converters): prepend reasoningContent blocks in _openai_to_bedrock()#419
jariy17 merged 2 commits 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
@cagataycali cagataycali requested a review from a team April 16, 2026 19:03
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 16, 2026

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #419   +/-   ##
=======================================
  Coverage        ?   91.17%           
=======================================
  Files           ?       58           
  Lines           ?     5018           
  Branches        ?      765           
=======================================
  Hits            ?     4575           
  Misses          ?      252           
  Partials        ?      191           
Flag Coverage Δ
unittests 91.17% <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

@phongtran222
Copy link
Copy Markdown

hi @cagataycali we having trouble in production with toolCall Arguments. Can you help me check it. Thank you
image

@jariy17 jariy17 temporarily deployed to manual-approval April 21, 2026 16:56 — with GitHub Actions Inactive
@jariy17 jariy17 temporarily deployed to manual-approval April 21, 2026 16:56 — with GitHub Actions Inactive
@jariy17 jariy17 deployed to manual-approval April 21, 2026 16:56 — with GitHub Actions Active
@jariy17 jariy17 temporarily deployed to manual-approval April 21, 2026 16:56 — with GitHub Actions Inactive
@jariy17
Copy link
Copy Markdown
Contributor

jariy17 commented Apr 21, 2026

Hi @phongtran222, Please report this as a issue and we can fix it from there.

@jariy17 jariy17 merged commit 667ef55 into aws:main Apr 21, 2026
25 of 26 checks passed
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

4 participants