Skip to content

Commit 8c76701

Browse files
authored
Merge pull request #1204 from aparekh02/swarmsapi1
[BUG-FIX] Swarm Router Bug Fix
2 parents 347846d + 8c20762 commit 8c76701

4 files changed

Lines changed: 58 additions & 18 deletions

File tree

docs/quickstart.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ task = "Write a short story about a robot who discovers music."
290290
# --- Example 1: SequentialWorkflow ---
291291
# Agents run one after another in a chain: Writer -> Editor -> Reviewer.
292292
print("Running a Sequential Workflow...")
293-
sequential_router = SwarmRouter(swarm_type=SwarmType.SequentialWorkflow, agents=agents)
293+
sequential_router = SwarmRouter(swarm_type="SequentialWorkflow", agents=agents)
294294
sequential_output = sequential_router.run(task)
295295
print(f"Final Sequential Output:\n{sequential_output}\n")
296296

297297
# --- Example 2: ConcurrentWorkflow ---
298298
# All agents receive the same initial task and run at the same time.
299299
print("Running a Concurrent Workflow...")
300-
concurrent_router = SwarmRouter(swarm_type=SwarmType.ConcurrentWorkflow, agents=agents)
300+
concurrent_router = SwarmRouter(swarm_type="ConcurrentWorkflow", agents=agents)
301301
concurrent_outputs = concurrent_router.run(task)
302302
# This returns a dictionary of each agent's output
303303
for agent_name, output in concurrent_outputs.items():
@@ -312,9 +312,9 @@ aggregator = Agent(
312312
model_name="gpt-4o-mini"
313313
)
314314
moa_router = SwarmRouter(
315-
swarm_type=SwarmType.MixtureOfAgents,
315+
swarm_type="MixtureOfAgents",
316316
agents=agents,
317-
aggregator_agent=aggregator, # MoA requires an aggregator
317+
aggregator_agent=aggregator,
318318
)
319319
aggregated_output = moa_router.run(task)
320320
print(f"Final Aggregated Output:\n{aggregated_output}\n")

docs/swarms/examples/swarm_router.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ GROQ_API_KEY=""
2929

3030
```python
3131
from swarms import Agent
32-
from swarms.structs.swarm_router import SwarmRouter, SwarmType
32+
from swarms.structs.swarm_router import SwarmRouter
3333

3434
# Initialize specialized agents
3535
data_extractor_agent = Agent(
@@ -61,7 +61,7 @@ sequential_router = SwarmRouter(
6161
name="SequentialRouter",
6262
description="Process tasks in sequence",
6363
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
64-
swarm_type=SwarmType.SequentialWorkflow,
64+
swarm_type="SequentialWorkflow",
6565
max_loops=1
6666
)
6767

@@ -76,7 +76,7 @@ concurrent_router = SwarmRouter(
7676
name="ConcurrentRouter",
7777
description="Process tasks concurrently",
7878
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
79-
swarm_type=SwarmType.ConcurrentWorkflow,
79+
swarm_type="ConcurrentWorkflow",
8080
max_loops=1
8181
)
8282

@@ -91,8 +91,8 @@ rearrange_router = SwarmRouter(
9191
name="RearrangeRouter",
9292
description="Dynamically rearrange agents for optimal task processing",
9393
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
94-
swarm_type=SwarmType.AgentRearrange,
95-
flow=f"{data_extractor_agent.agent_name} -> {summarizer_agent.agent_name} -> {financial_analyst_agent.agent_name}",
94+
swarm_type="AgentRearrange",
95+
rearrange_flow=f"{data_extractor_agent.agent_name} -> {summarizer_agent.agent_name} -> {financial_analyst_agent.agent_name}",
9696
max_loops=1
9797
)
9898

@@ -107,7 +107,7 @@ mixture_router = SwarmRouter(
107107
name="MixtureRouter",
108108
description="Combine multiple expert agents",
109109
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
110-
swarm_type=SwarmType.MixtureOfAgents,
110+
swarm_type="MixtureOfAgents",
111111
max_loops=1
112112
)
113113

@@ -137,14 +137,35 @@ router = SwarmRouter(
137137
name="CustomRouter",
138138
description="Custom router configuration",
139139
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
140-
swarm_type=SwarmType.SequentialWorkflow,
140+
swarm_type="SequentialWorkflow",
141141
max_loops=3,
142142
autosave=True,
143143
verbose=True,
144144
output_type="json"
145145
)
146146
```
147147

148+
# SwarmType Reference
149+
150+
## Valid SwarmType Values
151+
152+
| Value | Description |
153+
|-------|-------------|
154+
| `"SequentialWorkflow"` | Execute agents in sequence |
155+
| `"ConcurrentWorkflow"` | Execute agents concurrently |
156+
| `"AgentRearrange"` | Dynamically rearrange agent execution order |
157+
| `"MixtureOfAgents"` | Combine outputs from multiple agents |
158+
| `"GroupChat"` | Enable group chat between agents |
159+
| `"MultiAgentRouter"` | Route tasks to appropriate agents |
160+
| `"AutoSwarmBuilder"` | Automatically build swarm configuration |
161+
| `"HiearchicalSwarm"` | Hierarchical agent organization |
162+
| `"MajorityVoting"` | Use majority voting for decisions |
163+
| `"MALT"` | Multi-Agent Learning and Training |
164+
| `"CouncilAsAJudge"` | Council-based evaluation system |
165+
| `"InteractiveGroupChat"` | Interactive group chat with agents |
166+
| `"HeavySwarm"` | Heavy swarm for complex tasks |
167+
| `"auto"` | Automatically select swarm type |
168+
148169
# Best Practices
149170

150171
## Choose the appropriate swarm type based on your task requirements:
@@ -187,7 +208,7 @@ Here's a complete example showing how to use SwarmRouter in a real-world scenari
187208
```python
188209
import os
189210
from swarms import Agent
190-
from swarms.structs.swarm_router import SwarmRouter, SwarmType
211+
from swarms.structs.swarm_router import SwarmRouter
191212

192213
# Initialize specialized agents
193214
research_agent = Agent(
@@ -216,7 +237,7 @@ router = SwarmRouter(
216237
name="ResearchAnalysisRouter",
217238
description="Process research and analysis tasks",
218239
agents=[research_agent, analysis_agent, summary_agent],
219-
swarm_type=SwarmType.SequentialWorkflow,
240+
swarm_type="SequentialWorkflow",
220241
max_loops=1,
221242
verbose=True
222243
)

docs/swarms/structs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ task = "Write a short story about a robot who discovers music."
186186
# --- Example 1: SequentialWorkflow ---
187187
# Agents run one after another in a chain: Writer -> Editor -> Reviewer.
188188
print("Running a Sequential Workflow...")
189-
sequential_router = SwarmRouter(swarm_type=SwarmType.SequentialWorkflow, agents=agents)
189+
sequential_router = SwarmRouter(swarm_type="SequentialWorkflow", agents=agents)
190190
sequential_output = sequential_router.run(task)
191191
print(f"Final Sequential Output:\n{sequential_output}\n")
192192

193193
# --- Example 2: ConcurrentWorkflow ---
194194
# All agents receive the same initial task and run at the same time.
195195
print("Running a Concurrent Workflow...")
196-
concurrent_router = SwarmRouter(swarm_type=SwarmType.ConcurrentWorkflow, agents=agents)
196+
concurrent_router = SwarmRouter(swarm_type="ConcurrentWorkflow", agents=agents)
197197
concurrent_outputs = concurrent_router.run(task)
198198
# This returns a dictionary of each agent's output
199199
for agent_name, output in concurrent_outputs.items():
@@ -208,9 +208,9 @@ aggregator = Agent(
208208
model_name="gpt-4o-mini"
209209
)
210210
moa_router = SwarmRouter(
211-
swarm_type=SwarmType.MixtureOfAgents,
211+
swarm_type="MixtureOfAgents",
212212
agents=agents,
213-
aggregator_agent=aggregator, # MoA requires an aggregator
213+
aggregator_agent=aggregator,
214214
)
215215
aggregated_output = moa_router.run(task)
216216
print(f"Final Aggregated Output:\n{aggregated_output}\n")

swarms/structs/swarm_router.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import os
44
import traceback
5-
from typing import Any, Callable, Dict, List, Literal, Optional, Union
5+
from typing import Any, Callable, Dict, List, Literal, Optional, Union, get_args
66

77
from pydantic import BaseModel, Field
88

@@ -46,6 +46,7 @@
4646
"CouncilAsAJudge",
4747
"InteractiveGroupChat",
4848
"HeavySwarm",
49+
"BatchedGridWorkflow",
4950
]
5051

5152

@@ -272,6 +273,24 @@ def reliability_check(self):
272273
"SwarmRouter: Swarm type cannot be 'none'. Check the docs for all the swarm types available. https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
273274
)
274275

276+
# Validate swarm type is a valid string
277+
valid_swarm_types = get_args(SwarmType)
278+
279+
if not isinstance(self.swarm_type, str):
280+
raise SwarmRouterConfigError(
281+
f"SwarmRouter: swarm_type must be a string, not {type(self.swarm_type).__name__}. "
282+
f"Valid types are: {', '.join(valid_swarm_types)}. "
283+
"Use swarm_type='SequentialWorkflow' (string), NOT SwarmType.SequentialWorkflow. "
284+
"See https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
285+
)
286+
287+
if self.swarm_type not in valid_swarm_types:
288+
raise SwarmRouterConfigError(
289+
f"SwarmRouter: Invalid swarm_type '{self.swarm_type}'. "
290+
f"Valid types are: {', '.join(valid_swarm_types)}. "
291+
"See https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
292+
)
293+
275294
if (
276295
self.swarm_type != "HeavySwarm"
277296
and self.agents is None

0 commit comments

Comments
 (0)