1
+ from typing import Literal , Optional , Union , get_args , get_origin
2
+
1
3
from langgraph .graph import START , MessagesState , StateGraph
2
4
from langgraph .pregel import Pregel
3
- from typing_extensions import Any , Literal , Optional , Type , TypeVar , Union , get_args , get_origin
5
+ from typing_extensions import Any , TypeVar
4
6
5
7
from langgraph_swarm .handoff import get_handoff_destinations
6
8
@@ -11,18 +13,18 @@ class SwarmState(MessagesState):
11
13
# NOTE: this state field is optional and is not expected to be provided by the user.
12
14
# If a user does provide it, the graph will start from the specified active agent.
13
15
# If active agent is typed as a `str`, we turn it into enum of all active agent names.
14
- active_agent : Optional [ str ]
16
+ active_agent : str | None
15
17
16
18
17
19
StateSchema = TypeVar ("StateSchema" , bound = SwarmState )
18
- StateSchemaType = Type [StateSchema ]
20
+ StateSchemaType = type [StateSchema ]
19
21
20
22
21
23
def _update_state_schema_agent_names (
22
- state_schema : StateSchemaType , agent_names : list [str ]
24
+ state_schema : StateSchemaType ,
25
+ agent_names : list [str ],
23
26
) -> StateSchemaType :
24
27
"""Update the state schema to use Literal with agent names for 'active_agent'."""
25
-
26
28
active_agent_annotation = state_schema .__annotations__ ["active_agent" ]
27
29
28
30
# Check if the annotation is str or Optional[str]
@@ -120,14 +122,17 @@ def add(a: int, b: int) -> int:
120
122
config,
121
123
)
122
124
```
125
+
123
126
"""
124
127
channels = builder .schemas [builder .state_schema ]
125
128
if "active_agent" not in channels :
126
- raise ValueError ("Missing required key 'active_agent' in in builder's state_schema" )
129
+ msg = "Missing required key 'active_agent' in in builder's state_schema"
130
+ raise ValueError (msg )
127
131
128
132
if default_active_agent not in route_to :
133
+ msg = f"Default active agent '{ default_active_agent } ' not found in routes { route_to } "
129
134
raise ValueError (
130
- f"Default active agent ' { default_active_agent } ' not found in routes { route_to } "
135
+ msg ,
131
136
)
132
137
133
138
def route_to_active_agent (state : dict ):
@@ -142,7 +147,7 @@ def create_swarm(
142
147
* ,
143
148
default_active_agent : str ,
144
149
state_schema : StateSchemaType = SwarmState ,
145
- config_schema : Type [Any ] | None = None ,
150
+ config_schema : type [Any ] | None = None ,
146
151
) -> StateGraph :
147
152
"""Create a multi-agent swarm.
148
153
@@ -200,10 +205,12 @@ def add(a: int, b: int) -> int:
200
205
config,
201
206
)
202
207
```
208
+
203
209
"""
204
210
active_agent_annotation = state_schema .__annotations__ .get ("active_agent" )
205
211
if active_agent_annotation is None :
206
- raise ValueError ("Missing required key 'active_agent' in state_schema" )
212
+ msg = "Missing required key 'active_agent' in state_schema"
213
+ raise ValueError (msg )
207
214
208
215
agent_names = [agent .name for agent in agents ]
209
216
state_schema = _update_state_schema_agent_names (state_schema , agent_names )
0 commit comments