Skip to content

run_group_chat_iter

autogen.agentchat.run_group_chat_iter #

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

Run a group chat with iterator-based stepped execution.

Iterate over events as they occur. The background thread blocks after each event until you advance to the next iteration.

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

yield_on

List of event types to yield. If None, yields all events. Common types include TextEvent, ToolCallEvent, GroupChatRunChatEvent, and TerminationEvent.

TYPE: Sequence[type[BaseEvent]] | None DEFAULT: None

RETURNS DESCRIPTION
RunIterResponse

An iterator that yields events as they occur.

TYPE: RunIterResponse

Source code in autogen/agentchat/group/multi_agent_chat.py
@export_module("autogen.agentchat")
def run_group_chat_iter(
    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,
    yield_on: Sequence[type["BaseEvent"]] | None = None,
) -> RunIterResponse:
    """Run a group chat with iterator-based stepped execution.

    Iterate over events as they occur. The background thread blocks after each
    event until you advance to the next iteration.

    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.
        yield_on: List of event types to yield. If None, yields all events.
            Common types include TextEvent, ToolCallEvent, GroupChatRunChatEvent,
            and TerminationEvent.

    Returns:
        RunIterResponse: An iterator that yields events as they occur.
    """
    all_agents = pattern.agents + ([pattern.user_agent] if pattern.user_agent else [])

    def create_thread(iostream: ThreadIOStream) -> threading.Thread:
        def _initiate_group_chat() -> 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:
                    iostream.send(ErrorEvent(error=e))  # type: ignore[call-arg]

        return threading.Thread(target=_initiate_group_chat, daemon=True)

    return RunIterResponse(
        start_thread_func=create_thread,
        yield_on=yield_on,
        agents=all_agents,
    )