autogen icon indicating copy to clipboard operation
autogen copied to clipboard

[Bug] BaseGroupChat.run() and run_stream() reject valid Sequence[BaseChatMessage] (e.g. tuples) despite type annotations

Open Tanmay9223 opened this issue 4 weeks ago • 1 comments

What happened?

Describe the bug In autogen_agentchat.teams._group_chat._base_group_chat.BaseGroupChat, both run() and run_stream() declare the parameter type:

task: str | BaseChatMessage | Sequence[BaseChatMessage] | None = None

and the method docstrings state:

task (str | BaseChatMessage | Sequence[BaseChatMessage] | None): The task to run the team with. Can be a string, a single BaseChatMessage, or a list of BaseChatMessage.

However, in _base_group_chat.py (lines 463–472), the runtime type validation checks specifically for list:

elif isinstance(task, list):
    if not task:
        raise ValueError("Task list cannot be empty.")
    messages = []
    for msg in task:
        if not isinstance(msg, BaseChatMessage):
            raise ValueError("All messages in task list must be valid BaseChatMessage types")
        messages.append(msg)
else:
    raise ValueError("Task must be a string, a BaseChatMessage, or a list of BaseChatMessage.")

When passing any non-list Sequence[BaseChatMessage] — such as a tuple of messages ((TextMessage(...), ...)), a custom immutable collection, or a slice/sequence returned from another pipeline — the validation check falls through into the else branch and raises: ValueError: Task must be a string, a BaseChatMessage, or a list of BaseChatMessage.

To Reproduce

from autogen_agentchat.messages import TextMessage
from autogen_agentchat.teams._group_chat._base_group_chat import BaseGroupChat

# Pass a tuple of BaseChatMessage conforming to Sequence[BaseChatMessage]
task = (TextMessage(content="Hello", source="user"),)

# Checking the branch in BaseGroupChat.run_stream:
if task is None:
    print("None")
elif isinstance(task, str):
    print("str")
elif isinstance(task, list):
    print("list")
else:
    print("REJECTED:", type(task).__name__)
    # Raises: ValueError: Task must be a string, a BaseChatMessage, or a list of BaseChatMessage.

Output:

REJECTED: tuple

Expected behavior BaseGroupChat.run() and run_stream() should accept any Sequence[BaseChatMessage] (e.g. isinstance(task, Sequence) and not isinstance(task, str)), validating that each element is an instance of BaseChatMessage, consistent with the documented and advertised type annotation.

Screenshots N/A

Additional context PR #4500 introduced support for multiple messages in task inputs, but used isinstance(task, list) instead of isinstance(task, Sequence).

Which packages was the bug in?

  • Python AgentChat (autogen-agentchat>=0.4.0)

AutoGen library version.

Python dev (main branch)

Python version

3.11

Operating system

MacOS

Tanmay9223 avatar Nov 03 '26 15:11 Tanmay9223

Root cause is in \BaseGroupChat.run_stream\ (the single place \ un()\ delegates to): the validation uses \isinstance(task, list), so any non-list \Sequence[BaseChatMessage]\ (e.g. a tuple) falls through to the \lse\ branch and raises, even though the signature and docstring already promise \Sequence[BaseChatMessage].

Fix is a one-line change to \isinstance(task, Sequence)\ (the module already imports \Sequence\ from \ yping) plus updating the fallback error string. I'll add a regression test next to the existing \ est_round_robin_group_chat_with_message_list\ and open a PR shortly.

Wang-Zephyr avatar Nov 08 '26 18:11 Wang-Zephyr