From 70c7c5b17c42e8835ba5f566d612d832bab783f3 Mon Sep 17 00:00:00 2001 From: cagataycali Date: Thu, 16 Apr 2026 14:58:15 -0400 Subject: [PATCH] fix(converters): prepend reasoningContent blocks in _openai_to_bedrock() 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 #416 --- .../memory/integrations/strands/converters/openai.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bedrock_agentcore/memory/integrations/strands/converters/openai.py b/src/bedrock_agentcore/memory/integrations/strands/converters/openai.py index 2810f30b..e5acfc4f 100644 --- a/src/bedrock_agentcore/memory/integrations/strands/converters/openai.py +++ b/src/bedrock_agentcore/memory/integrations/strands/converters/openai.py @@ -77,6 +77,7 @@ def _openai_to_bedrock(openai_msg: dict) -> dict: """Convert an OpenAI message dict to Strands-native message shape.""" role = openai_msg.get("role", "user") content_items: list[dict[str, Any]] = [] + reasoning_items: list[dict[str, Any]] = [] if role == "tool": tool_result: dict[str, Any] = { @@ -119,11 +120,13 @@ def _openai_to_bedrock(openai_msg: dict) -> dict: 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) bedrock_role = "assistant" if role == "assistant" else "user" - 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} class OpenAIConverseConverter: