From 6a01ce0737a0a3b291f0b892a60bc9de60ddf90d Mon Sep 17 00:00:00 2001 From: Shyam Sathish <150350918+ShyamSathish005@users.noreply.github.com> Date: Tue, 13 May 2025 13:10:32 +0000 Subject: [PATCH] fix: ensure Bedrock compliance by separating tool result and conversation blocks in chat history adapter --- .../_sk_chat_completion_adapter.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py b/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py index 78f0aa5a24de..23f4b292dc2d 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py @@ -293,27 +293,27 @@ def __init__( self._tools_plugin: KernelPlugin = KernelPlugin(name="autogen_tools") def _convert_to_chat_history(self, messages: Sequence[LLMMessage]) -> ChatHistory: - """Convert Autogen LLMMessages to SK ChatHistory""" + """Convert Autogen LLMMessages to SK ChatHistory, ensuring Bedrock compliance (no conversation and tool result in same turn)""" chat_history = ChatHistory() + last_was_tool = False for msg in messages: if msg.type == "SystemMessage": chat_history.add_system_message(msg.content) + last_was_tool = False elif msg.type == "UserMessage": if isinstance(msg.content, str): chat_history.add_user_message(msg.content) else: - # Handle list of str/Image - convert to string for now chat_history.add_user_message(str(msg.content)) + last_was_tool = False elif msg.type == "AssistantMessage": # Check if it's a function-call style message if isinstance(msg.content, list) and all(isinstance(fc, FunctionCall) for fc in msg.content): - # If there's a 'thought' field, you can add that as plain assistant text if msg.thought: chat_history.add_assistant_message(msg.thought) - function_call_contents: list[FunctionCallContent] = [] for fc in msg.content: function_call_contents.append( @@ -325,15 +325,14 @@ def _convert_to_chat_history(self, messages: Sequence[LLMMessage]) -> ChatHistor arguments=fc.arguments, ) ) - - # Mark the assistant's message as tool-calling chat_history.add_assistant_message( function_call_contents, finish_reason=FinishReason.TOOL_CALLS, ) + last_was_tool = False else: - # Plain assistant text chat_history.add_assistant_message(msg.content) + last_was_tool = False elif msg.type == "FunctionExecutionResultMessage": # Add each function result as a separate tool message @@ -347,8 +346,14 @@ def _convert_to_chat_history(self, messages: Sequence[LLMMessage]) -> ChatHistor result=result.content, ) ) - # A single "tool" message with one or more results + # Bedrock: ensure tool result is not combined with conversation in same turn + if last_was_tool: + # Insert a dummy user message to break the turn if previous was also a tool + chat_history.add_user_message(".") chat_history.add_tool_message(tool_results) + last_was_tool = True + else: + last_was_tool = False return chat_history