{"record":{"id":"09905d14d412794d","repo":"microsoft/semantic-kernel","slug":"agent-agent-name-cannot-handoff-to-itself","errorCode":null,"errorMessage":"Agent {agent_name} cannot handoff to itself.","messagePattern":"Agent (.+?) cannot handoff to itself\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/orchestration/handoffs.py","lineNumber":527,"sourceCode":"        The type is appended with the internal topic type to ensure uniqueness in the runtime\n        that may be shared by multiple orchestrations.\n        \"\"\"\n        return f\"{agent.name}_{internal_topic_type}\"\n\n    def _validate_handoffs(self) -> None:\n        \"\"\"Validate the handoffs to ensure all connections are valid.\"\"\"\n        if not self._handoffs:\n            raise ValueError(\"Handoffs cannot be empty. Please provide at least one handoff connection.\")\n\n        member_names = {m.name for m in self._members}\n        for agent_name, connections in self._handoffs.items():\n            if agent_name not in member_names:\n                raise ValueError(f\"Agent {agent_name} is not a member of the handoff group.\")\n            for handoff_agent_name in connections:\n                if handoff_agent_name not in member_names:\n                    raise ValueError(f\"Agent {handoff_agent_name} is not a member of the handoff group.\")\n                if handoff_agent_name == agent_name:\n                    raise ValueError(f\"Agent {agent_name} cannot handoff to itself.\")\n\n\n# endregion HandoffOrchestration\n","sourceCodeStart":509,"sourceCodeEnd":531,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/orchestration/handoffs.py#L509-L531","documentation":"Raised as a ValueError in _validate_handoffs when a handoff connection's target equals its source (agent hands off to itself). A self-handoff creates an infinite routing loop with no progress, so the validator explicitly rejects it.","triggerScenarios":"A Handoff(agent_x, agent_x) is provided where both source and target resolve to the same agent name. Can happen when handoffs are generated programmatically (e.g. all-pairs) without excluding the diagonal, or via a copy-paste mistake.","commonSituations":"Auto-generating handoffs in a loop over members and forgetting to skip self-pairs; copy-pasting a Handoff and forgetting to change the target; accidentally adding a reflexive edge when refactoring.","solutions":["Remove any handoff where source == target.","When generating handoffs programmatically, skip the case where the two agents are identical.","Review the handoff list for reflexive edges before constructing the orchestration."],"exampleFix":"# before\nhandoffs = [Handoff(agent_a, agent_a), Handoff(agent_a, agent_b)]\n# after - drop the self-handoff\nhandoffs = [Handoff(agent_a, agent_b)]","handlingStrategy":"validation","validationCode":"# Reject self-handoffs before constructing:\nfor h in handoffs:\n    src = h.source.name if hasattr(h.source, \"name\") else h.source\n    tgt = h.target.name if hasattr(h.target, \"name\") else h.target\n    assert src != tgt, f\"Agent '{src}' cannot hand off to itself\"\nhandoffs = [h for h in handoffs if source_of(h) != target_of(h)]","typeGuard":null,"tryCatchPattern":"try:\n    orchestration = HandoffOrchestration(members=members, handoffs=handoffs)\nexcept ValueError as ex:\n    if \"cannot handoff to itself\" in str(ex):\n        handoffs = [h for h in handoffs if name(h.source) != name(h.target)]\n        orchestration = HandoffOrchestration(members=members, handoffs=handoffs)","preventionTips":["When generating handoffs programmatically, skip self-pairs.","Review the handoff list for reflexive edges before construction.","Add a unit test asserting no source equals its target."],"tags":["orchestration","handoff","validation","configuration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}