Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions python/packages/core/agent_framework/_workflows/_handoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ def _target_from_tool_name(name: str | None) -> str | None:
return None


def _has_tool_result_for_call(conversation: list[ChatMessage], call_id: str) -> bool:
"""Check if a tool result message exists for the given call_id.

Args:
conversation: The conversation history to check
call_id: The function call ID to look for

Returns:
True if a tool result with matching call_id exists, False otherwise
"""
for msg in conversation:
if msg.role != Role.TOOL:
continue

for content in msg.contents:
if isinstance(content, FunctionResultContent):
if content.call_id == call_id:
return True

return False


class _HandoffCoordinator(BaseGroupChatOrchestrator):
"""Coordinates agent-to-agent transfers and user turn requests."""

Expand Down Expand Up @@ -482,6 +504,14 @@ def _append_tool_acknowledgement(
if not call_id:
return

# Skip if tool result already exists to avoid duplicates from _AutoHandoffMiddleware
if _has_tool_result_for_call(conversation, call_id):
logger.debug(
f"Tool result for call_id '{call_id}' already exists, "
f"skipping duplicate for handoff to '{resolved_id}'"
)
return

result_payload: Any = {"handoff_to": resolved_id}
result_content = FunctionResultContent(call_id=call_id, result=result_payload)
tool_message = ChatMessage(
Expand Down