{"record":{"id":"eb90972fdc5a2b07","repo":"microsoft/autogen","slug":"termination-condition-has-already-been-reached-eb9097","errorCode":null,"errorMessage":"Termination condition has already been reached.","messagePattern":"Termination condition has already been reached\\.","errorType":"exception","errorClass":"TerminatedException","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py","lineNumber":39,"sourceCode":"    pass\n\n\nclass StopMessageTermination(TerminationCondition, Component[StopMessageTerminationConfig]):\n    \"\"\"Terminate the conversation if a StopMessage is received.\"\"\"\n\n    component_config_schema = StopMessageTerminationConfig\n    component_provider_override = \"autogen_agentchat.conditions.StopMessageTermination\"\n\n    def __init__(self) -> None:\n        self._terminated = False\n\n    @property\n    def terminated(self) -> bool:\n        return self._terminated\n\n    async def __call__(self, messages: Sequence[BaseAgentEvent | BaseChatMessage]) -> StopMessage | None:\n        if self._terminated:\n            raise TerminatedException(\"Termination condition has already been reached\")\n        for message in messages:\n            if isinstance(message, StopMessage):\n                self._terminated = True\n                return StopMessage(content=\"Stop message received\", source=\"StopMessageTermination\")\n        return None\n\n    async def reset(self) -> None:\n        self._terminated = False\n\n    def _to_config(self) -> StopMessageTerminationConfig:\n        return StopMessageTerminationConfig()\n\n    @classmethod\n    def _from_config(cls, config: StopMessageTerminationConfig) -> Self:\n        return cls()\n\n\nclass MaxMessageTerminationConfig(BaseModel):","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-agentchat/src/autogen_agentchat/conditions/_terminations.py#L21-L57","documentation":"StopMessageTermination terminates when it sees a StopMessage in the batch. Once _terminated is set, any further call raises TerminatedException. Reset clears the flag.","triggerScenarios":"A StopMessage appeared in a previous batch (e.g. emitted by a handoff-aware agent or another termination source inside a team), then the condition is evaluated again — typically a second team.run() without reset, or the StopMessage-producing agent and this condition wired into an And composite that is re-invoked.","commonSituations":"Teams where agents emit StopMessage for early exit; reusing a one-shot condition across runs; inspecting conditions by calling them in custom orchestration code.","solutions":["Check condition.terminated before calling","await condition.reset() / await team.reset() between runs","In custom loops, break out of iteration immediately once a StopMessage is returned"],"exampleFix":"# before\nresult = await team.run(task=t1)\nresult = await team.run(task=t2)  # StopMessageTermination still terminated\n\n# after\nawait team.reset()\nresult = await team.run(task=t2)","handlingStrategy":"validation","validationCode":"if not cond.terminated:\n    stop = await cond(messages)","typeGuard":null,"tryCatchPattern":"from autogen_agentchat.conditions import TerminatedException\ntry:\n    stop = await cond(messages)\nexcept TerminatedException:\n    await cond.reset()\n    stop = await cond(messages)","preventionTips":["Reset teams/conditions between runs","Break your orchestration loop as soon as a StopMessage is produced","Check .terminated before calling"],"tags":["termination","stop-message","stateful","agentchat"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}