Skip to content

Commit c1115eb

Browse files
authored
update langgraph & pass state explicitly in handoffs (#20)
1 parent 4874dd9 commit c1115eb

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@ By default, the agents in the swarm are assumed to use handoff tools created wit
116116
* add tool call arguments for the LLM to populate, for example a task description for the next agent
117117
* 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
118118

119-
> [!IMPORTANT]
120-
> If you want to change what messages are passed to the next agent, you **must** use a different state schema key for `messages` in your agent implementation (e.g., `alice_messages`). By default, all agent (subgraph) state updates are applied to the swarm (parent) graph state during the handoff. Since all of the agents by default are assumed to communicate over a single `messages` key, this means that the agent's messages are **automatically combined** into the parent graph's `messages`, unless an agent uses a different key for `messages`. See more on this in the [customizing agent implementation](#customizing-agent-implementation) section.
121-
122119
Here is an example of what a custom handoff tool might look like:
123120

124121
```python
@@ -170,7 +167,7 @@ def create_custom_handoff_tool(*, agent_name: str, tool_name: str, tool_descript
170167

171168
### Customizing agent implementation
172169

173-
By default, individual agents are expected to communicate over a single `messages` key that is shared by all agents and the overall multi-agent swarm graph. This means that **all** of the messages from **all** of the agents will be combined into a single, shared list of messages. This might not be desirable if you don't want to expose an agent's internal history of messages. To change this, you can customize the agent by taking the following steps:
170+
By default, individual agents are expected to communicate over a single `messages` key that is shared by all agents and the overall multi-agent swarm graph. This means that messages from **all** of the agents will be combined into a single, shared list of messages. This might not be desirable if you don't want to expose an agent's internal history of messages. To change this, you can customize the agent by taking the following steps:
174171

175172
1. use custom [state schema](https://langchain-ai.github.io/langgraph/concepts/low_level#schema) with a different key for messages, for example `alice_messages`
176173
1. write a wrapper that converts the parent graph state to the child agent state and back (see this [how-to](https://langchain-ai.github.io/langgraph/how-tos/subgraph-transform-state/) guide)

langgraph_swarm/handoff.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from langchain_core.messages import ToolMessage
44
from langchain_core.tools import BaseTool, InjectedToolCallId, tool
55
from langgraph.graph.state import CompiledStateGraph
6-
from langgraph.prebuilt import ToolNode
6+
from langgraph.prebuilt import InjectedState, ToolNode
77
from langgraph.types import Command
88
from typing_extensions import Annotated
99

@@ -34,6 +34,7 @@ def create_handoff_tool(*, agent_name: str, description: str | None = None) -> B
3434

3535
@tool(name, description=description)
3636
def handoff_to_agent(
37+
state: Annotated[dict, InjectedState],
3738
tool_call_id: Annotated[str, InjectedToolCallId],
3839
):
3940
tool_message = ToolMessage(
@@ -44,7 +45,7 @@ def handoff_to_agent(
4445
return Command(
4546
goto=agent_name,
4647
graph=Command.PARENT,
47-
update={"messages": [tool_message], "active_agent": agent_name},
48+
update={"messages": state["messages"] + [tool_message], "active_agent": agent_name},
4849
)
4950

5051
handoff_to_agent.metadata = {METADATA_KEY_HANDOFF_DESTINATION: agent_name}

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ build-backend = "pdm.backend"
44

55
[project]
66
name = "langgraph-swarm"
7-
version = "0.0.4"
7+
version = "0.0.5"
88
description = "An implementation of a multi-agent swarm using LangGraph"
99
authors = [
1010
{name = "Vadym Barda", email = "[email protected] "}
1111
]
1212
readme = "README.md"
1313
requires-python = ">=3.10"
1414
dependencies = [
15-
"langgraph>=0.3.1,<0.4.0",
15+
"langgraph>=0.3.5,<0.4.0",
1616
"langchain-core>=0.3.40,<0.4.0"
1717
]
1818

uv.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)