microsoft/autogen · error · RuntimeError
The team cannot be loaded while it is running.
Error message
The team cannot be loaded while it is running.
What it means
load_state() overwrites every participant's state, which is unsafe while messages are flowing: it initializes if needed, then refuses with RuntimeError if _is_running is True. The flag is set for the whole duration of the load itself and cleared in a finally block.
Source
Thrown at python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py:809
# save_state method because we want to support saving state of remote agents.
agent_states[name] = await self._runtime.agent_save_state(agent_id)
# Save the state of the group chat manager.
agent_id = AgentId(type=self._group_chat_manager_topic_type, key=self._team_id)
agent_states[self._group_chat_manager_name] = await self._runtime.agent_save_state(agent_id)
return TeamState(agent_states=agent_states).model_dump()
async def load_state(self, state: Mapping[str, Any]) -> None:
"""Load an external state and overwrite the current state of the group chat team.
The state is loaded by calling the :meth:`~autogen_core.AgentRuntime.agent_load_state` method
on each participant and the group chat manager with their internal agent ID.
See :meth:`~autogen_agentchat.teams.BaseGroupChat.save_state` for the expected format of the state.
"""
if not self._initialized:
await self._init(self._runtime)
if self._is_running:
raise RuntimeError("The team cannot be loaded while it is running.")
self._is_running = True
try:
team_state = TeamState.model_validate(state)
# Load the state of all participants.
for name, agent_type in zip(self._participant_names, self._participant_topic_types, strict=True):
agent_id = AgentId(type=agent_type, key=self._team_id)
if name not in team_state.agent_states:
raise ValueError(f"Agent state for {name} not found in the saved state.")
await self._runtime.agent_load_state(agent_id, team_state.agent_states[name])
# Load the state of the group chat manager.
agent_id = AgentId(type=self._group_chat_manager_topic_type, key=self._team_id)
if self._group_chat_manager_name not in team_state.agent_states:
raise ValueError(f"Agent state for {self._group_chat_manager_name} not found in the saved state.")
await self._runtime.agent_load_state(agent_id, team_state.agent_states[self._group_chat_manager_name])
except ValidationError as e:
raise ValueError(View on GitHub (pinned to 027ecf0a37)
Solutions
- Complete or stop the active run first (exhaust the stream or await stream.aclose()).
- Use one asyncio.Lock to make run/load_state/reset mutually exclusive per team.
- For per-request workloads, instantiate a fresh team and load state into it instead of mutating a running one.
Example fix
# before
asyncio.gather(team.run(task="a"), team.load_state(saved)) # one of them raises
# after
lock = asyncio.Lock()
async with lock:
await team.run(task="a")
async with lock:
await team.load_state(saved) Defensive patterns
Strategy: validation
Validate before calling
async with team_lock:
await team.load_state(state) # same lock used for run/reset Try / catch
try:
await team.load_state(state)
except RuntimeError as e:
if "cannot be loaded while it is running" in str(e):
await active_stream.aclose()
await team.load_state(state)
else:
raise Prevention
- Serialize run, reset, load_state, and save_state behind one asyncio.Lock per team.
- For multi-session apps, build a fresh team per session and load state into it.
- Never fire load_state from a callback that can race an active stream.
When it happens
Trigger: Calling await team.load_state(state) while a run_stream generator is active (started but not exhausted/closed), or from a concurrent coroutine during await team.run().
Common situations: Hot-swapping conversation state from a UI 'switch session' action while a run is streaming; concurrent tasks sharing one team instance for run and checkpoint restore.
Related errors
- The team is already running, it cannot run again until it is
- The group chat is currently running. It must be stopped befo
- The group chat has not been initialized. It must be run befo
- The group chat has not been initialized. It must be run befo
- Agent state for {name} not found in the saved state.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/4c777ff182efc525.
Report an issue: GitHub.