Skip to content

Handoffs

autogen.agentchat.group.handoffs.Handoffs #

Bases: BaseModel

Container for all handoff transition conditions of a ConversableAgent.

Three types of conditions can be added, each with a different order and time of use: 1. OnContextConditions (evaluated without an LLM) 2. OnConditions (evaluated with an LLM) 3. After work TransitionTarget (if no other transition is triggered)

Supports method chaining: agent.handoffs.add_context_conditions([condition1]) .add_llm_condition(condition2) .set_after_work(after_work)

context_conditions class-attribute instance-attribute #

context_conditions = Field(default_factory=list)

llm_conditions class-attribute instance-attribute #

llm_conditions = Field(default_factory=list)

after_work class-attribute instance-attribute #

after_work = None

add_context_condition #

add_context_condition(condition)

Add a single context condition.

PARAMETER DESCRIPTION
condition

The OnContextCondition to add

TYPE: OnContextCondition

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add_context_condition(self, condition: OnContextCondition) -> "Handoffs":
    """
    Add a single context condition.

    Args:
        condition: The OnContextCondition to add

    Returns:
        Self for method chaining
    """
    # Validate that it is an OnContextCondition
    if not isinstance(condition, OnContextCondition):
        raise TypeError(f"Expected an OnContextCondition instance, got {type(condition).__name__}")

    self.context_conditions.append(condition)
    return self

add_context_conditions #

add_context_conditions(conditions)

Add multiple context conditions.

PARAMETER DESCRIPTION
conditions

List of OnContextConditions to add

TYPE: list[OnContextCondition]

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add_context_conditions(self, conditions: list[OnContextCondition]) -> "Handoffs":
    """
    Add multiple context conditions.

    Args:
        conditions: List of OnContextConditions to add

    Returns:
        Self for method chaining
    """
    # Validate that it is a list of OnContextConditions
    if not all(isinstance(condition, OnContextCondition) for condition in conditions):
        raise TypeError("All conditions must be of type OnContextCondition")

    self.context_conditions.extend(conditions)
    return self

add_llm_condition #

add_llm_condition(condition)

Add a single LLM condition.

PARAMETER DESCRIPTION
condition

The OnCondition to add

TYPE: OnCondition

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add_llm_condition(self, condition: OnCondition) -> "Handoffs":
    """
    Add a single LLM condition.

    Args:
        condition: The OnCondition to add

    Returns:
        Self for method chaining
    """
    # Validate that it is an OnCondition
    if not isinstance(condition, OnCondition):
        raise TypeError(f"Expected an OnCondition instance, got {type(condition).__name__}")

    self.llm_conditions.append(condition)
    return self

add_llm_conditions #

add_llm_conditions(conditions)

Add multiple LLM conditions.

PARAMETER DESCRIPTION
conditions

List of OnConditions to add

TYPE: list[OnCondition]

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add_llm_conditions(self, conditions: list[OnCondition]) -> "Handoffs":
    """
    Add multiple LLM conditions.

    Args:
        conditions: List of OnConditions to add

    Returns:
        Self for method chaining
    """
    # Validate that it is a list of OnConditions
    if not all(isinstance(condition, OnCondition) for condition in conditions):
        raise TypeError("All conditions must be of type OnCondition")

    self.llm_conditions.extend(conditions)
    return self

set_after_work #

set_after_work(target)

Set the after work target (only one allowed).

PARAMETER DESCRIPTION
target

The after work TransitionTarget to set

TYPE: TransitionTarget

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def set_after_work(self, target: TransitionTarget) -> "Handoffs":
    """
    Set the after work target (only one allowed).

    Args:
        target: The after work TransitionTarget to set

    Returns:
        Self for method chaining
    """
    if not isinstance(target, TransitionTarget):
        raise TypeError(f"Expected a TransitionTarget instance, got {type(target).__name__}")

    self.after_work = target
    return self

add #

add(condition: OnContextCondition) -> Handoffs
add(condition: OnCondition) -> Handoffs
add(condition)

Add a single condition (OnContextCondition or OnCondition).

PARAMETER DESCRIPTION
condition

The condition to add (OnContextCondition or OnCondition)

TYPE: Union[OnContextCondition, OnCondition]

RAISES DESCRIPTION
TypeError

If the condition type is not supported

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add(self, condition: Union[OnContextCondition, OnCondition]) -> "Handoffs":
    """
    Add a single condition (OnContextCondition or OnCondition).

    Args:
        condition: The condition to add (OnContextCondition or OnCondition)

    Raises:
        TypeError: If the condition type is not supported

    Returns:
        Self for method chaining
    """
    # This add method is a helper method designed to make it easier for
    # adding handoffs without worrying about the specific type.
    if isinstance(condition, OnContextCondition):
        return self.add_context_condition(condition)
    elif isinstance(condition, OnCondition):
        return self.add_llm_condition(condition)
    else:
        raise TypeError(f"Unsupported condition type: {type(condition).__name__}")

add_many #

add_many(conditions)

Add multiple conditions of any supported types (OnContextCondition and OnCondition).

PARAMETER DESCRIPTION
conditions

List of conditions to add

TYPE: list[Union[OnContextCondition, OnCondition]]

RAISES DESCRIPTION
TypeError

If an unsupported condition type is provided

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def add_many(self, conditions: list[Union[OnContextCondition, OnCondition]]) -> "Handoffs":
    """
    Add multiple conditions of any supported types (OnContextCondition and OnCondition).

    Args:
        conditions: List of conditions to add

    Raises:
        TypeError: If an unsupported condition type is provided

    Returns:
        Self for method chaining
    """
    # This add_many method is a helper method designed to make it easier for
    # adding handoffs without worrying about the specific type.
    context_conditions = []
    llm_conditions = []

    for condition in conditions:
        if isinstance(condition, OnContextCondition):
            context_conditions.append(condition)
        elif isinstance(condition, OnCondition):
            llm_conditions.append(condition)
        else:
            raise TypeError(f"Unsupported condition type: {type(condition).__name__}")

    if context_conditions:
        self.add_context_conditions(context_conditions)
    if llm_conditions:
        self.add_llm_conditions(llm_conditions)

    return self

clear #

clear()

Clear all handoff conditions.

RETURNS DESCRIPTION
Handoffs

Self for method chaining

Source code in autogen/agentchat/group/handoffs.py
def clear(self) -> "Handoffs":
    """
    Clear all handoff conditions.

    Returns:
        Self for method chaining
    """
    self.context_conditions.clear()
    self.llm_conditions.clear()
    self.after_work = None
    return self

get_llm_conditions_by_target_type #

get_llm_conditions_by_target_type(target_type)

Get OnConditions for a specific target type.

PARAMETER DESCRIPTION
target_type

The type of condition to retrieve

TYPE: type

RETURNS DESCRIPTION
list[OnCondition]

List of conditions of the specified type, or None if none exist

Source code in autogen/agentchat/group/handoffs.py
def get_llm_conditions_by_target_type(self, target_type: type) -> list[OnCondition]:
    """
    Get OnConditions for a specific target type.

    Args:
        target_type: The type of condition to retrieve

    Returns:
        List of conditions of the specified type, or None if none exist
    """
    return [on_condition for on_condition in self.llm_conditions if on_condition.has_target_type(target_type)]

get_context_conditions_by_target_type #

get_context_conditions_by_target_type(target_type)

Get OnContextConditions for a specific target type.

PARAMETER DESCRIPTION
target_type

The type of condition to retrieve

TYPE: type

RETURNS DESCRIPTION
list[OnContextCondition]

List of conditions of the specified type, or None if none exist

Source code in autogen/agentchat/group/handoffs.py
def get_context_conditions_by_target_type(self, target_type: type) -> list[OnContextCondition]:
    """
    Get OnContextConditions for a specific target type.

    Args:
        target_type: The type of condition to retrieve

    Returns:
        List of conditions of the specified type, or None if none exist
    """
    return [
        on_context_condition
        for on_context_condition in self.context_conditions
        if on_context_condition.has_target_type(target_type)
    ]

get_llm_conditions_requiring_wrapping #

get_llm_conditions_requiring_wrapping()

Get LLM conditions that have targets that require wrapping.

RETURNS DESCRIPTION
list[OnCondition]

List of LLM conditions that require wrapping

Source code in autogen/agentchat/group/handoffs.py
def get_llm_conditions_requiring_wrapping(self) -> list[OnCondition]:
    """
    Get LLM conditions that have targets that require wrapping.

    Returns:
        List of LLM conditions that require wrapping
    """
    return [condition for condition in self.llm_conditions if condition.target_requires_wrapping()]

get_context_conditions_requiring_wrapping #

get_context_conditions_requiring_wrapping()

Get context conditions that have targets that require wrapping.

RETURNS DESCRIPTION
list[OnContextCondition]

List of context conditions that require wrapping

Source code in autogen/agentchat/group/handoffs.py
def get_context_conditions_requiring_wrapping(self) -> list[OnContextCondition]:
    """
    Get context conditions that have targets that require wrapping.

    Returns:
        List of context conditions that require wrapping
    """
    return [condition for condition in self.context_conditions if condition.target_requires_wrapping()]

set_llm_function_names #

set_llm_function_names()

Set the LLM function names for all LLM conditions, creating unique names for each function.

Source code in autogen/agentchat/group/handoffs.py
def set_llm_function_names(self) -> None:
    """
    Set the LLM function names for all LLM conditions, creating unique names for each function.
    """
    for i, condition in enumerate(self.llm_conditions):
        # Function names are made unique and allow multiple OnCondition's to the same agent
        condition.llm_function_name = f"transfer_to_{condition.target.normalized_name()}_{i + 1}"