microsoft/semantic-kernel · error · Exception

Unable to proceed while another agent is active.

Error message

Unable to proceed while another agent is active.

What it means

Raised by AgentChat.set_activity_or_throw() with a bare Exception when _is_active is already True. AgentChat uses a non-reentrant activity flag protected by a threading.Lock to serialize chat operations (add_chat_messages, invoke_agent, get_chat_messages, reset). If one operation is still in flight (e.g. an async generator not fully consumed), the flag remains True and the next operation throws.

Source

Thrown at python/semantic_kernel/agents/group_chat/agent_chat.py:45

    broadcast_queue: BroadcastQueue = Field(default_factory=BroadcastQueue)
    agent_channels: dict[str, AgentChannel] = Field(default_factory=dict)
    channel_map: dict[Agent, str] = Field(default_factory=dict)
    history: ChatHistory = Field(default_factory=ChatHistory)

    _lock: threading.Lock = PrivateAttr(default_factory=threading.Lock)
    _is_active: bool = False

    @property
    def is_active(self) -> bool:
        """Indicates whether the agent is currently active."""
        return self._is_active

    def set_activity_or_throw(self):
        """Set the activity signal or throw an exception if another agent is active."""
        with self._lock:
            if self._is_active:
                raise Exception("Unable to proceed while another agent is active.")
            self._is_active = True

    def clear_activity_signal(self):
        """Clear the activity signal."""
        with self._lock:
            self._is_active = False

    def invoke(self, agent: Agent | None = None, is_joining: bool = True) -> AsyncIterable[ChatMessageContent]:
        """Invoke the agent asynchronously."""
        raise NotImplementedError("Subclasses should implement this method")

    async def get_messages_in_descending_order(self) -> AsyncIterable[ChatMessageContent]:
        """Get messages in descending order asynchronously."""
        for index in range(len(self.history.messages) - 1, -1, -1):
            yield self.history.messages[index]
            await asyncio.sleep(0)  # Yield control to the event loop

    async def get_chat_messages(self, agent: "Agent | None" = None) -> AsyncIterable[ChatMessageContent]:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Fully consume every async generator from invoke/invoke_stream/get_chat_messages before starting the next operation.
  2. Serialize operations with an asyncio.Lock at the call site if concurrent access is needed.
  3. If the flag is stuck due to an abandoned generator, call chat.reset() or manually chat.clear_activity_signal() to recover.
  4. Avoid sharing one AgentChat/AgentGroupChat instance across concurrent tasks.

Example fix

# before — generator abandoned early, _is_active stays True
async for msg in chat.invoke_agent(agent):
    print(msg.content)
    break  # generator not exhausted
await chat.add_chat_message("next")  # raises

# after — exhaust the generator or use anexplicit lock
async for msg in chat.invoke_agent(agent):
    print(msg.content)
# generator fully consumed, activity flag cleared
await chat.add_chat_message("next")
Defensive patterns

Strategy: validation

Validate before calling

if chat.is_active:
    raise RuntimeError("Chat is active; finish or cancel the current operation first")
await chat.add_chat_message("hello")

Try / catch

try:
    await chat.add_chat_message("hello")
except Exception as exc:
    if "another agent is active" in str(exc):
        chat.clear_activity_signal()  # recover from a stuck flag
        await chat.add_chat_message("hello")
    raise

Prevention

When it happens

Trigger: Calling a second AgentChat operation while the first is still active: e.g. starting chat.add_chat_messages then, before it completes, calling chat.invoke_agent; or not fully iterating an async generator from invoke/get_chat_messages before starting another operation; concurrent/awaited tasks sharing the same AgentChat instance.

Common situations: Not fully consuming an async generator (breaking out of 'async for' early leaves the activity flag set until GC runs the finally); concurrent asyncio tasks using the same AgentGroupChat; calling add_chat_message inside a loop that also invokes agents without awaiting completion.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/46a3b324bf7096b0. Report an issue: GitHub.