1+ from typing import Literal , Optional , Union , get_args , get_origin
2+
13from langgraph .graph import START , MessagesState , StateGraph
24from 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
46
57from langgraph_swarm .handoff import get_handoff_destinations
68
@@ -11,18 +13,18 @@ class SwarmState(MessagesState):
1113 # NOTE: this state field is optional and is not expected to be provided by the user.
1214 # If a user does provide it, the graph will start from the specified active agent.
1315 # 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
1517
1618
1719StateSchema = TypeVar ("StateSchema" , bound = SwarmState )
18- StateSchemaType = Type [StateSchema ]
20+ StateSchemaType = type [StateSchema ]
1921
2022
2123def _update_state_schema_agent_names (
22- state_schema : StateSchemaType , agent_names : list [str ]
24+ state_schema : StateSchemaType ,
25+ agent_names : list [str ],
2326) -> StateSchemaType :
2427 """Update the state schema to use Literal with agent names for 'active_agent'."""
25-
2628 active_agent_annotation = state_schema .__annotations__ ["active_agent" ]
2729
2830 # Check if the annotation is str or Optional[str]
@@ -120,14 +122,17 @@ def add(a: int, b: int) -> int:
120122 config,
121123 )
122124 ```
125+
123126 """
124127 channels = builder .schemas [builder .state_schema ]
125128 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 )
127131
128132 if default_active_agent not in route_to :
133+ msg = f"Default active agent '{ default_active_agent } ' not found in routes { route_to } "
129134 raise ValueError (
130- f"Default active agent ' { default_active_agent } ' not found in routes { route_to } "
135+ msg ,
131136 )
132137
133138 def route_to_active_agent (state : dict ):
@@ -142,7 +147,7 @@ def create_swarm(
142147 * ,
143148 default_active_agent : str ,
144149 state_schema : StateSchemaType = SwarmState ,
145- config_schema : Type [Any ] | None = None ,
150+ config_schema : type [Any ] | None = None ,
146151) -> StateGraph :
147152 """Create a multi-agent swarm.
148153
@@ -200,10 +205,12 @@ def add(a: int, b: int) -> int:
200205 config,
201206 )
202207 ```
208+
203209 """
204210 active_agent_annotation = state_schema .__annotations__ .get ("active_agent" )
205211 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 )
207214
208215 agent_names = [agent .name for agent in agents ]
209216 state_schema = _update_state_schema_agent_names (state_schema , agent_names )
0 commit comments