Fix Bedrock connector ChatHistory broken with parallel tool calls#13649
Open
jgarrison929 wants to merge 1 commit intomicrosoft:mainfrom
Open
Fix Bedrock connector ChatHistory broken with parallel tool calls#13649jgarrison929 wants to merge 1 commit intomicrosoft:mainfrom
jgarrison929 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…icrosoft#13647) When using Bedrock with models that support parallel tool calling (e.g. Claude Sonnet), SK creates separate ChatMessageContent items for each tool call and each tool result. Bedrock's Converse API requires all tool_use blocks from one assistant turn in a single message and all tool_result blocks in a single user message. Changes: - BedrockModelUtilities.BuildMessageList: Rewritten to handle FunctionCallContent (-> ToolUse blocks), FunctionResultContent (-> ToolResult blocks), and merge consecutive same-role messages. - BedrockModelUtilities.MapAuthorRoleToConversationRole: Handle AuthorRole.Tool by mapping to ConversationRole.User. - BedrockChatCompletionClient.CreateChatMessageContentItemCollection: Handle ToolUse blocks in responses by creating FunctionCallContent items, enabling the auto-invoke tool calling loop. - Added comprehensive unit tests for parallel tool call scenarios. Fixes microsoft#13647
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #13647
When using the Bedrock connector with models that support parallel tool calling (e.g., Claude Sonnet 4.5), SK creates separate
ChatMessageContentitems for each tool call and each tool result. However, the Bedrock Converse API requires:toolUseblocks from one assistant turn in a single messagetoolResultblocks for those calls in a single user messageThe current
BuildMessageListmethod creates one BedrockMessageperChatMessageContent, which causes Bedrock to reject the request with:Description
BedrockModelUtilities.cs:BuildMessageList: Rewritten to iterate through chat history items, build properContentBlocklists for each message type (FunctionCallContent→ToolUseBlock,FunctionResultContent→ToolResultBlock,TextContent→ text block), and merge consecutive messages with the same Bedrock role into a single message.MapAuthorRoleToConversationRole: Added handling forAuthorRole.Toolby mapping toConversationRole.User(Bedrock expects tool results as user messages).BuildContentBlocks(new private method): ConvertsChatMessageContent.Itemsto BedrockContentBlockobjects.ConvertArgumentsToDocument/ConvertValueToDocument(new private methods): ConvertKernelArgumentstoAmazon.Runtime.Documents.DocumentforToolUseBlock.Input.BedrockChatCompletionClient.cs:CreateChatMessageContentItemCollection: Updated to handleToolUseblocks in responses by creatingFunctionCallContentitems (previously only createdTextContent). This enables the auto-invoke tool calling loop to work correctly.ParseDocumentToArguments/DocumentToObject(new private methods): Convert BedrockDocumentresponses back toKernelArguments.Contribution Checklist