Skip to content

Commit 4186297

Browse files
authored
update handoff to include names (#51)
1 parent dee33cf commit 4186297

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ By default, the agents in the swarm are assumed to use handoff tools created wit
114114

115115
* change tool name and/or description
116116
* add tool call arguments for the LLM to populate, for example a task description for the next agent
117-
* change what data is passed to the next agent as part of the handoff: by default `create_handoff_tool` passes **full** message history (all of the messages generated in the swarm up to this point), as well as the contents of `Command.update` to the next agent
117+
* change what data is passed to the next agent as part of the handoff: by default `create_handoff_tool` passes **full** message history (all of the messages generated in the swarm up to this point), as well as a tool message indicating successful handoff.
118118

119119
Here is an example of what a custom handoff tool might look like:
120120

@@ -126,9 +126,9 @@ from langchain_core.messages import ToolMessage
126126
from langgraph.types import Command
127127
from langgraph.prebuilt import InjectedState
128128

129-
def create_custom_handoff_tool(*, agent_name: str, tool_name: str, tool_description: str) -> BaseTool:
129+
def create_custom_handoff_tool(*, agent_name: str, name: str | None, description: str | None) -> BaseTool:
130130

131-
@tool(name=tool_name, description=tool_description)
131+
@tool(name, description=description)
132132
def handoff_to_agent(
133133
# you can add additional tool call arguments for the LLM to populate
134134
# for example, you can ask the LLM to populate a task description for the next agent
@@ -139,18 +139,18 @@ def create_custom_handoff_tool(*, agent_name: str, tool_name: str, tool_descript
139139
):
140140
tool_message = ToolMessage(
141141
content=f"Successfully transferred to {agent_name}",
142-
name=tool_name,
142+
name=name,
143143
tool_call_id=tool_call_id,
144144
)
145145
# you can use a different messages state key here, if your agent uses a different schema
146146
# e.g., "alice_messages" instead of "messages"
147-
last_agent_message = state["messages"][-1]
147+
messages = state["messages"]
148148
return Command(
149149
goto=agent_name,
150150
graph=Command.PARENT,
151151
# NOTE: this is a state update that will be applied to the swarm multi-agent graph (i.e., the PARENT graph)
152152
update={
153-
"messages": [last_agent_message, tool_message],
153+
"messages": messages + [tool_message],
154154
"active_agent": agent_name,
155155
# optionally pass the task description to the next agent
156156
"task_description": task_description,

langgraph_swarm/handoff.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def _normalize_agent_name(agent_name: str) -> str:
1616
return WHITESPACE_RE.sub("_", agent_name.strip()).lower()
1717

1818

19-
def create_handoff_tool(*, agent_name: str, description: str | None = None) -> BaseTool:
19+
def create_handoff_tool(
20+
*, agent_name: str, name: str | None = None, description: str | None = None
21+
) -> BaseTool:
2022
"""Create a tool that can handoff control to the requested agent.
2123
2224
Args:
@@ -26,9 +28,14 @@ def create_handoff_tool(*, agent_name: str, description: str | None = None) -> B
2628
although you are only limited to the names accepted by LangGraph
2729
nodes as well as the tool names accepted by LLM providers
2830
(the tool name will look like this: `transfer_to_<agent_name>`).
31+
name: Optional name of the tool to use for the handoff.
32+
If not provided, the tool name will be `transfer_to_<agent_name>`.
2933
description: Optional description for the handoff tool.
34+
If not provided, the tool description will be `Ask agent <agent_name> for help`.
3035
"""
31-
name = f"transfer_to_{_normalize_agent_name(agent_name)}"
36+
if name is None:
37+
name = f"transfer_to_{_normalize_agent_name(agent_name)}"
38+
3239
if description is None:
3340
description = f"Ask agent '{agent_name}' for help"
3441

0 commit comments

Comments
 (0)