Skip to content

run_group_chat

autogen.agentchat.run_group_chat #

run_group_chat(pattern, messages, max_rounds=20, safeguard_policy=None, safeguard_llm_config=None, mask_llm_config=None)

Run a group chat with multiple agents using the specified pattern.

This method executes a multi-agent conversation in a background thread and returns immediately with a RunResponse object that can be used to iterate over events.

For step-by-step execution with control over each event, use run_group_chat_iter() instead.

PARAMETER DESCRIPTION
pattern

The pattern that defines how agents interact (e.g., AutoPattern, RoundRobinPattern, RandomPattern).

TYPE: Pattern

messages

The initial message(s) to start the conversation. Can be a string or a list of message dictionaries.

TYPE: list[dict[str, Any]] | str

max_rounds

Maximum number of conversation rounds. Defaults to 20.

TYPE: int DEFAULT: 20

safeguard_policy

Optional safeguard policy for content filtering.

TYPE: dict[str, Any] | str | None DEFAULT: None

safeguard_llm_config

Optional LLM config for safeguard evaluation.

TYPE: LLMConfig | None DEFAULT: None

mask_llm_config

Optional LLM config for content masking.

TYPE: LLMConfig | None DEFAULT: None

RETURNS DESCRIPTION
RunResponseProtocol

RunResponseProtocol

Source code in autogen/agentchat/group/multi_agent_chat.py
@export_module("autogen.agentchat")
def run_group_chat(
    pattern: "Pattern",
    messages: list[dict[str, Any]] | str,
    max_rounds: int = 20,
    safeguard_policy: dict[str, Any] | str | None = None,
    safeguard_llm_config: LLMConfig | None = None,
    mask_llm_config: LLMConfig | None = None,
) -> RunResponseProtocol:
    """Run a group chat with multiple agents using the specified pattern.

    This method executes a multi-agent conversation in a background thread and returns
    immediately with a RunResponse object that can be used to iterate over events.

    For step-by-step execution with control over each event, use run_group_chat_iter() instead.

    Args:
        pattern: The pattern that defines how agents interact (e.g., AutoPattern,
            RoundRobinPattern, RandomPattern).
        messages: The initial message(s) to start the conversation. Can be a string
            or a list of message dictionaries.
        max_rounds: Maximum number of conversation rounds. Defaults to 20.
        safeguard_policy: Optional safeguard policy for content filtering.
        safeguard_llm_config: Optional LLM config for safeguard evaluation.
        mask_llm_config: Optional LLM config for content masking.

    Returns:
        RunResponseProtocol
    """
    iostream = ThreadIOStream()
    all_agents = pattern.agents + ([pattern.user_agent] if pattern.user_agent else [])
    response = RunResponse(iostream, agents=all_agents)

    def _initiate_group_chat(
        pattern: "Pattern" = pattern,
        messages: list[dict[str, Any]] | str = messages,
        max_rounds: int = max_rounds,
        safeguard_policy: dict[str, Any] | str | None = safeguard_policy,
        safeguard_llm_config: LLMConfig | None = safeguard_llm_config,
        mask_llm_config: LLMConfig | None = mask_llm_config,
        iostream: ThreadIOStream = iostream,
        response: RunResponse = response,
    ) -> None:
        with IOStream.set_default(iostream):
            try:
                chat_result, context_vars, agent = initiate_group_chat(
                    pattern=pattern,
                    messages=messages,
                    max_rounds=max_rounds,
                    safeguard_policy=safeguard_policy,
                    safeguard_llm_config=safeguard_llm_config,
                    mask_llm_config=mask_llm_config,
                )

                IOStream.get_default().send(
                    RunCompletionEvent(  # type: ignore[call-arg]
                        history=chat_result.chat_history,
                        summary=chat_result.summary,
                        cost=chat_result.cost,
                        last_speaker=agent.name,
                        context_variables=context_vars,
                    )
                )
            except Exception as e:
                response.iostream.send(ErrorEvent(error=e))  # type: ignore[call-arg]

    threading.Thread(
        target=_initiate_group_chat,
    ).start()

    return response