Skip to content

a_run_group_chat_iter

autogen.agentchat.a_run_group_chat_iter #

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

Async version of run_group_chat_iter for async contexts.

Iterate over events as they occur using async for. 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
AsyncRunIterResponse

An async iterator that yields events as they occur.

TYPE: AsyncRunIterResponse

Source code in autogen/agentchat/group/multi_agent_chat.py
@export_module("autogen.agentchat")
def a_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,
) -> AsyncRunIterResponse:
    """Async version of run_group_chat_iter for async contexts.

    Iterate over events as they occur using async for. 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:
        AsyncRunIterResponse: An async 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:
        async def _async_initiate_group_chat() -> None:
            chat_result, context_vars, agent = await a_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.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,
                )
            )

        def _run_in_thread() -> None:
            with IOStream.set_default(iostream):
                try:
                    asyncio.run(_async_initiate_group_chat())
                except Exception as e:
                    iostream.send(ErrorEvent(error=e))  # type: ignore[call-arg]

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

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