microsoft/autogen · error · ValueError

The team is already running, it cannot run again until it is

Error message

The team is already running, it cannot run again until it is stopped.

What it means

A team cannot have two concurrent runs: _is_running is set True when run_stream starts and cleared only when the stream finishes. A second run() / run_stream() call while the first is still streaming raises this immediately to prevent interleaved message queues and corrupted state.

Source

Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py:484

            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.")
        # Check if the messages types are registered with the message factory.
        if messages is not None:
            for msg in messages:
                if not self._message_factory.is_registered(msg.__class__):
                    raise ValueError(
                        f"Message type {msg.__class__} is not registered with the message factory. "
                        "Please register it with the message factory by adding it to the "
                        "custom_message_types list when creating the team."
                    )

        if self._is_running:
            raise ValueError("The team is already running, it cannot run again until it is stopped.")
        self._is_running = True

        if self._embedded_runtime:
            # Start the embedded runtime.
            assert isinstance(self._runtime, SingleThreadedAgentRuntime)
            self._runtime.start()

        if not self._initialized:
            await self._init(self._runtime)

        shutdown_task: asyncio.Task[None] | None = None
        if self._embedded_runtime:

            async def stop_runtime() -> None:
                assert isinstance(self._runtime, SingleThreadedAgentRuntime)
                try:
                    # This will propagate any exceptions raised.
                    await self._runtime.stop_when_idle()

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Ensure the previous run fully completes: iterate run_stream to exhaustion or await the run() coroutine before starting a new one.
  2. When abandoning a stream early, explicitly close the generator: await stream.aclose().
  3. Serialize access with an asyncio.Lock around team runs, or create a fresh team instance per concurrent request.

Example fix

# before
stream = team.run_stream(task="a")
async for msg in stream: break  # abandoned -> _is_running stays True
await team.run(task="b")  # ValueError

# after
stream = team.run_stream(task="a")
async for msg in stream: break
await stream.aclose()  # releases _is_running
await team.run(task="b")
Defensive patterns

Strategy: try-catch

Validate before calling

team_lock = asyncio.Lock()  # serialize all runs per team
async with team_lock:
    result = await team.run(task="hi")

Try / catch

try:
    result = await team.run(task=task)
except ValueError as e:
    if "already running" in str(e):
        # wait for current run, then retry once
        await current_run_task
        result = await team.run(task=task)
    else:
        raise

Prevention

When it happens

Trigger: Awaiting team.run() twice concurrently (asyncio.gather(team.run(...), team.run(...))), or calling run() on a team whose earlier run_stream generator was started but never fully consumed/closed — an abandoned generator leaves _is_running True until GC finalizes the generator's finally block.

Common situations: Two coroutines sharing one team object; a FastAPI/queue handler that starts a new run before the previous response stream completed; abandoning a run_stream async generator early (breaking out of the async for loop without closing it).

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/1f09697c517d1424. Report an issue: GitHub.