Skip to content

fix: ensure Bedrock compliance by separating tool result and conversation blocks in chat history adapter #6522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ShyamSathish005
Copy link
Contributor

Why are these changes needed?

This PR updates the _convert_to_chat_history method in SKChatCompletionAdapter to ensure that tool result blocks and conversation blocks are never combined in the same turn. If two tool result messages would be consecutive, a dummy user message is inserted to break the turn, fully complying with Bedrock's requirements and preventing the API error.

Fixes intermittent Bedrock ValidationException errors in multi-agent scenarios.
Ensures stable tool use and message handoff for teams of agents using AWS Bedrock models via Semantic Kernel.

Related issue number

Closes #6480

Checks

@ekzhu ekzhu requested review from victordibia and removed request for victordibia May 14, 2025 16:44
@victordibia
Copy link
Collaborator

victordibia commented May 14, 2025

@ShyamSathish005 ,

Thanks for the PR.
Can you kindly include a minimum reproducible example in the PR description?
Ideally an example triggers the error and fixed by the _sk_chat_completion_adapter.py update

Thanks.

@ShyamSathish005
Copy link
Contributor Author

ShyamSathish005 commented May 15, 2025

Sure.

@ShyamSathish005
Copy link
Contributor Author

This PR updates the _convert_to_chat_history method in SKChatCompletionAdapter to ensure strict Bedrock compliance by separating tool result and conversation blocks in the chat history. The adapter now inserts a dummy user message if two tool result messages would otherwise be consecutive, preventing them from being combined in the same turn. This change addresses intermittent ValidationException errors from Bedrock in multi-agent scenarios, ensuring stable tool use and message handoff for agent teams using AWS Bedrock models via Semantic Kernel.

The following example triggers the error before this fix:

Example that triggers the Bedrock ValidationException

from your_package import SKChatCompletionAdapter

chat_history = [
    {"role": "user", "content": "Start"},
    {"role": "tool", "content": "Tool result 1"},
    {"role": "tool", "content": "Tool result 2"},  # Consecutive tool results
    {"role": "assistant", "content": "Response"}
]

adapter = SKChatCompletionAdapter()
try:
    # This would raise a ValidationException before the fix
    converted = adapter._convert_to_chat_history(chat_history)
    print(converted)
except Exception as e:
    print("Error:", e)

#6522 After the fix:
The above code will no longer raise a Bedrock ValidationException. The adapter will insert a dummy user message between consecutive tool results, ensuring compliance and stable operation.

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.

Unhandled Exception while running Swarm Team with AWS Bedrock Nova models using semantic Kernel Adapter
2 participants