agentchat.contrib.swarm_agent
AFTER_WORK
Handles the next step in the conversation when an agent doesn’t suggest a tool call or a handoff
Arguments:
agent
- The agent to hand off to or the after work option. Can be a SwarmAgent, a string name of a SwarmAgent, an AfterWorkOption, or a Callable. The Callable signature is: def my_after_work_func(last_speaker: SwarmAgent, messages: List[Dict[str, Any]], groupchat: GroupChat) -> Union[AfterWorkOption, SwarmAgent, str]:
ON_CONDITION
Defines a condition for transitioning to another agent or nested chats
Arguments:
target
- The agent to hand off to or the nested chat configuration. Can be a SwarmAgent or a Dict. If a Dict, it should follow the convention of the nested chat configuration, with the exception of a carryover configuration which is unique to Swarms. Swarm Nested chat documentation: https://ag2ai.github.io/ag2/docs/topics/swarm#registering-handoffs-to-a-nested-chatcondition
- The condition for transitioning to the target agent, evaluated by the LLM to determine whether to call the underlying function/tool which does the transition.available
- Optional condition to determine if this ON_CONDITION is available. Can be a Callable or a string. If a string, it will look up the value of the context variable with that name, which should be a bool.
UPDATE_SYSTEM_MESSAGE
Update the agent’s system message before they reply
Arguments:
update_function
- The string or function to update the agent’s system message. Can be a string or a Callable. If a string, it will be used as a template and substitute the context variables. If a Callable, it should have the signature: def my_update_function(agent: ConversableAgent, messages: List[Dict[str, Any]]) -> str
initiate_swarm_chat
Initialize and run a swarm chat
Arguments:
initial_agent
- The first receiving agent of the conversation.messages
- Initial message(s).agents
- List of swarm agents.user_agent
- Optional user proxy agent for falling back to.max_rounds
- Maximum number of conversation rounds.context_variables
- Starting context variables.after_work
- Method to handle conversation continuation when an agent doesn’t select the next agent. If no agent is selected and no tool calls are output, we will use this method to determine the next agent. Must be a AFTER_WORK instance (which is a dataclass accepting a SwarmAgent, AfterWorkOption, A str (of the AfterWorkOption)) or a callable. AfterWorkOption:- TERMINATE (Default): Terminate the conversation.
- REVERT_TO_USER : Revert to the user agent if a user agent is provided. If not provided, terminate the conversation.
- STAY : Stay with the last speaker.
Callable
- A custom function that takes the current agent, messages, and groupchat as arguments and returns an AfterWorkOption or a SwarmAgent (by reference or string name).
Returns:
ChatResult
- Conversations chat history. Dict[str, Any]: Updated Context variables.SwarmAgent
- Last speaker.
SwarmResult
Encapsulates the possible return values for a swarm agent function.
Arguments:
values
str - The result values as a string.agent
SwarmAgent - The swarm agent instance, if applicable.context_variables
dict - A dictionary of context variables.
SwarmAgent
Swarm agent for participating in a swarm.
SwarmAgent is a subclass of ConversableAgent.
Additional args: functions (List[Callable]): A list of functions to register with the agent. update_agent_state_before_reply (List[Callable]): A list of functions, including UPDATE_SYSTEM_MESSAGEs, called to update the agent before it replies.
register_update_agent_state_before_reply
Register functions that will be called when the agent is selected and before it speaks. You can add your own validation or precondition functions here.
Arguments:
functions
List[Callable[[], None]] - A list of functions to be registered. Each function is called when the agent is selected and before it speaks.
register_hand_off
Register a function to hand off to another agent.
Arguments:
-
hand_to
- A list of ON_CONDITIONs and an, optional, AFTER_WORK conditionHand off template: def transfer_to_agent_name() -> SwarmAgent: return agent_name
- register the function with the agent
- register the schema with the agent, description set to the condition
generate_swarm_tool_reply
Pre-processes and generates tool call replies.
This function:
- Adds context_variables back to the tool call for the function, if necessary.
- Generates the tool calls reply.
- Updates context_variables and next_agent based on the tool call response.
add_single_function
Add a single function to the agent, removing context variables for LLM use
process_nested_chat_carryover
Process carryover messages for a nested chat (typically for the first chat of a swarm)
The carryover_config key is a dictionary containing: “summary_method”: The method to use to summarise the messages, can be “all”, “last_msg”, “reflection_with_llm” or a Callable “summary_args”: Optional arguments for the summary method
Supported carryover ‘summary_methods’ are: “all” - all messages will be incorporated “last_msg” - the last message will be incorporated “reflection_with_llm” - an llm will summarise all the messages and the summary will be incorporated as a single message Callable - a callable with the signature: my_method(agent: ConversableAgent, messages: List[Dict[str, Any]]) -> str
Arguments:
chat
- The chat dictionary containing the carryover configurationrecipient
- The recipient agentmessages
- The messages from the parent chatsender
- The sender agenttrim_n_messages
- The number of latest messages to trim from the messages list