From 272f8190029caec3adea14099a2a589ec738265f Mon Sep 17 00:00:00 2001 From: Audric Schiltknecht Date: Mon, 15 Sep 2025 10:47:40 -0400 Subject: [PATCH] Python: Fix description not set in OrchestrationHandoffs when adding agent by name --- .../agents/orchestration/handoffs.py | 2 +- .../unit/agents/orchestration/test_handoff.py | 45 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/python/semantic_kernel/agents/orchestration/handoffs.py b/python/semantic_kernel/agents/orchestration/handoffs.py index 569c0598188a..bd0d1bca9247 100644 --- a/python/semantic_kernel/agents/orchestration/handoffs.py +++ b/python/semantic_kernel/agents/orchestration/handoffs.py @@ -87,7 +87,7 @@ def add(self, source_agent: str | Agent, target_agent: str | Agent, description: return self._add( source_agent=source_agent if isinstance(source_agent, str) else source_agent.name, target_agent=target_agent if isinstance(target_agent, str) else target_agent.name, - description=description or target_agent.description or "" if isinstance(target_agent, Agent) else "", + description=description or (target_agent.description or "" if isinstance(target_agent, Agent) else ""), ) def add_many(self, source_agent: str | Agent, target_agents: list[str | Agent] | AgentHandoffs) -> "Self": diff --git a/python/tests/unit/agents/orchestration/test_handoff.py b/python/tests/unit/agents/orchestration/test_handoff.py index 4797fc8bd792..7e21fb9805b1 100644 --- a/python/tests/unit/agents/orchestration/test_handoff.py +++ b/python/tests/unit/agents/orchestration/test_handoff.py @@ -264,6 +264,9 @@ def test_orchestration_handoff_add(): agent_a = MockAgent() agent_b = MockAgent() + agent_a.description = "Description Agent A" + agent_b.description = "Description Agent B" + orchestration_handoffs = OrchestrationHandoffs().add(agent_a, agent_b).add(agent_b, agent_a) assert isinstance(orchestration_handoffs, OrchestrationHandoffs) @@ -272,10 +275,48 @@ def test_orchestration_handoff_add(): assert len(orchestration_handoffs[agent_b.name]) == 1 for handoff_agent_name, handoff_description in orchestration_handoffs[agent_a.name].items(): assert handoff_agent_name == agent_b.name - assert handoff_description == "" + assert handoff_description == "Description Agent B" for handoff_agent_name, handoff_description in orchestration_handoffs[agent_b.name].items(): assert handoff_agent_name == agent_a.name - assert handoff_description == "" + assert handoff_description == "Description Agent A" + + +def test_orchestration_handoff_add_with_description(): + """Test the add method of the OrchestrationHandoffs.""" + agent_a = MockAgent() + agent_b = MockAgent() + + orchestration_handoffs = ( + OrchestrationHandoffs() + .add(agent_a, agent_b, "Description from Agent A to Agent B") + .add(agent_b, agent_a, "Description from Agent B to Agent A") + ) + + for handoff_agent_name, handoff_description in orchestration_handoffs[agent_a.name].items(): + assert handoff_agent_name == agent_b.name + assert handoff_description == "Description from Agent A to Agent B" + for handoff_agent_name, handoff_description in orchestration_handoffs[agent_b.name].items(): + assert handoff_agent_name == agent_a.name + assert handoff_description == "Description from Agent B to Agent A" + + +def test_orchestration_handoff_add_by_name_with_description(): + """Test the add method of the OrchestrationHandoffs.""" + agent_a = MockAgent() + agent_b = MockAgent() + + orchestration_handoffs = ( + OrchestrationHandoffs() + .add(agent_a.name, agent_b.name, "Description from Agent A to Agent B") + .add(agent_b.name, agent_a.name, "Description from Agent B to Agent A") + ) + + for handoff_agent_name, handoff_description in orchestration_handoffs[agent_a.name].items(): + assert handoff_agent_name == agent_b.name + assert handoff_description == "Description from Agent A to Agent B" + for handoff_agent_name, handoff_description in orchestration_handoffs[agent_b.name].items(): + assert handoff_agent_name == agent_a.name + assert handoff_description == "Description from Agent B to Agent A" def test_orchestration_handoff_add_many():