Skip to content

Group chat does not work with Anthropic model in Autogen #6794

Open
@ml-mitansh

Description

@ml-mitansh

What happened?

Describe the bug
An error occurred while processing a publish message in the PatchingReviewerAgent and PatchingCoderAgent.

Code
Here's the relevant code for creating team:

from autogen_agentchat.conditions import TextMentionTermination, SourceMatchTermination
from autogen_ext.models.anthropic import AnthropicBedrockChatCompletionClient, BedrockInfo
from autogen_core.models import ModelInfo

anthropic_client = AnthropicBedrockChatCompletionClient(
    model=os.getenv("AWS_BEDROCK_MODEL_ID"),
    temperature=0.1,
    model_info=ModelInfo(
        vision=False, function_calling=False, json_output=False, family="claude-3-haiku", structured_output=True
    ),
    bedrock_info=BedrockInfo(
        aws_access_key=os.getenv("AWS_ACCESS_KEY"),
        aws_secret_key=os.getenv("AWS_SECRET_KEY"),
        aws_region=os.getenv("AWS_REGION"),
        aws_session_token=os.getenv("AWS_SESSION_TOKEN"),
    ),
)

async def get_team(assistant: str, session_id: str, user_id: str) -> RoundRobinGroupChat:
    if assistant not in assistant_configs:
        raise ValueError(f"Invalid assistant name: {assistant}")

    # Create a shallow copy of the config dictionary
    agentcoder_config = dict(assistant_configs["PatchingCoderAgent"])
    agent_writecode = AssistantAgent(name="PatchingCoderAgent", model_client=anthropic_client, **agentcoder_config)

    agentreviewer_config = dict(assistant_configs["PatchingReviewerAgent"])
    agent_reviewcode = AssistantAgent(name="PatchingReviewerAgent", model_client=anthropic_client, **agentreviewer_config)

    text_termination = TextMentionTermination("APPROVED")
    source_termination = SourceMatchTermination("PatchingReviewerAgent")
    combined_termination = source_termination | text_termination

    team = RoundRobinGroupChat([agent_writecode, agent_reviewcode], termination_condition=combined_termination)
    
    # Get session-specific paths
    paths = get_session_path(user_id, session_id, assistant)
    state_path = paths["state_path"]

    # Load state if it exists
    if os.path.exists(state_path):
        async with aiofiles.open(state_path, "r") as file:
            state = json.loads(await file.read())
        await team.load_state(state)

    return team

request = [{'id': '3b756e50-ceab-4bbf-97db-6e9b5eb6a6f9', 'source': 'user', 'models_usage': None, 'metadata': {}, 'created_at': '2025-07-11T05:48:01.545295Z', 'content': 'Hi', 'type': 'TextMessage'}]
team = await get_team(assistant_name, session_id, user_id)
stream = team.run_stream(task=request)

Error
`Error processing publish message for PatchingReviewerAgent_abe7edf8-f184-4cca-8b54-e2c546b1137a/abe7edf8-f184-4cca-8b54-e2c546b1137a
Traceback (most recent call last):
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_single_threaded_agent_runtime.py", line 606, in _on_message
return await agent.on_message(
^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_base_agent.py", line 119, in on_message
return await self.on_message_impl(message, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\teams_group_chat_sequential_routed_agent.py", line 67, in on_message_impl
return await super().on_message_impl(message, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_routed_agent.py", line 485, in on_message_impl
return await h(self, message, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_routed_agent.py", line 268, in wrapper
return_value = await func(self, message, ctx) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\teams_group_chat_chat_agent_container.py", line 84, in handle_request
async for msg in self._agent.on_messages_stream(self._message_buffer, ctx.cancellation_token):
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\agents_assistant_agent.py", line 964, in on_messages_stream
async for inference_output in self._call_llm(
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\agents_assistant_agent.py", line 1118, in _call_llm
model_result = await model_client.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_ext\models\anthropic_anthropic_client.py", line 652, in create
result: Message = cast(Message, await future) # type: ignore
^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic\resources\messages\messages.py", line 2272, in create
return await self._post(
^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic_base_client.py", line 1888, in post
return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic_base_client.py", line 1688, in request
raise self._make_status_error_from_response(err.response) from None
anthropic.BadRequestError: Error code: 400 - {'message': 'messages: roles must alternate between "user" and "assistant", but found multiple "user" roles in a row'}
Error processing publish message for PatchingCoderAgent_abe7edf8-f184-4cca-8b54-e2c546b1137a/abe7edf8-f184-4cca-8b54-e2c546b1137a
Traceback (most recent call last):
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_single_threaded_agent_runtime.py", line 606, in _on_message
return await agent.on_message(
^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_base_agent.py", line 119, in on_message
return await self.on_message_impl(message, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\teams_group_chat_sequential_routed_agent.py", line 72, in on_message_impl
return await super().on_message_impl(message, ctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_core_routed_agent.py", line 486, in on_message_impl
return await self.on_unhandled_message(message, ctx) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\teams_group_chat_chat_agent_container.py", line 138, in on_unhandled_message
raise ValueError(f"Unhandled message in agent container: {type(message)}")
ValueError: Unhandled message in agent container: <class 'autogen_agentchat.teams._group_chat._events.GroupChatError'>
Error processing message: BadRequestError: Error code: 400 - {'message': 'messages: roles must alternate between "user" and "assistant", but found multiple "user" roles in a row'}
Traceback:
Traceback (most recent call last):

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\teams_group_chat_chat_agent_container.py", line 84, in handle_request
async for msg in self._agent.on_messages_stream(self._message_buffer, ctx.cancellation_token):

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\agents_assistant_agent.py", line 964, in on_messages_stream
async for inference_output in self._call_llm(

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_agentchat\agents_assistant_agent.py", line 1118, in _call_llm
model_result = await model_client.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\autogen_ext\models\anthropic_anthropic_client.py", line 652, in create
result: Message = cast(Message, await future) # type: ignore
^^^^^^^^^^^^

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic\resources\messages\messages.py", line 2272, in create
return await self._post(
^^^^^^^^^^^^^^^^^

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic_base_client.py", line 1888, in post
return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\ML Projects\bedrock-poc\backend.venv\Lib\site-packages\anthropic_base_client.py", line 1688, in request
raise self._make_status_error_from_response(err.response) from None

anthropic.BadRequestError: Error code: 400 - {'message': 'messages: roles must alternate between "user" and "assistant", but found multiple "user" roles in a row'}`

To Reproduce

  1. Set up a WebSocket server as in the code provided
  2. Connect from Browser, send a message

Expected behavior

  1. User sends a request (e.g., asking for a script or code change).
  2. PatchingCoderAgent (first agent) processes the request and generates a code script in response.
  3. PatchingReviewerAgent (second agent) receives the script from PatchingCoderAgent, reviews it, and provides feedback or improvements.

Which packages was the bug in?

Python Core (autogen-core)

AutoGen library version.

Python 0.6.2

Other library version.

No response

Model used

anthropic.claude-3-haiku-20240307-v1:0

Model provider

AWS Bedrock

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

Windows

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions