Summary
The autoswarm CLI command currently calls generate_swarm_config() which produces a YAML config and runs the swarm internally, but never persists a usable Python file for the user. The user has no artifact they can inspect, modify, or re-run without touching the CLI again.
This issue tracks adding a full pipeline to autoswarm that:
- Generates the config — LLM produces the agent/swarm YAML (already works via
generate_swarm_config / AutoSwarmBuilder)
- Parses the dict →
Agent() structs — map every key in the config dict to the correct Agent(...) constructor parameter
- Writes a ready-to-run
.py file in the current working directory so the user has a reusable, editable script
Current behavior (swarms/cli/main.py:55–100, swarms/agents/auto_generate_swarm_config.py)
swarms autoswarm --task "build a research pipeline" --model gpt-4.1
- Calls
generate_swarm_config(task=task, model=model)
- Config is generated, agents are created via
create_agents_from_yaml(), swarm runs
- Nothing is written to disk; the user has no output file
Desired behavior
swarms autoswarm --task "build a research pipeline" --model gpt-4.1
Expected output:
✓ Swarm configuration generated
✓ Parsed 3 agents from config
✓ Written to: ./autoswarm_research_pipeline.py
Generated file (autoswarm_research_pipeline.py):
from swarms import Agent
from swarms.structs.swarm_router import SwarmRouter
# Auto-generated by `swarms autoswarm` — edit freely
researcher = Agent(
agent_name="Researcher",
agent_description="Finds and summarises relevant papers",
system_prompt="You are a research specialist ...",
model_name="gpt-4.1",
max_loops=1,
temperature=0.5,
)
analyst = Agent(
agent_name="Analyst",
agent_description="Synthesises findings into actionable insights",
system_prompt="You are a data analyst ...",
model_name="gpt-4.1",
max_loops=2,
temperature=0.3,
)
writer = Agent(
agent_name="Writer",
agent_description="Produces the final report",
system_prompt="You are a technical writer ...",
model_name="gpt-4.1",
max_loops=1,
)
swarm = SwarmRouter(
name="Research-Pipeline",
description="End-to-end research pipeline",
agents=[researcher, analyst, writer],
swarm_type="SequentialWorkflow",
max_loops=1,
)
if __name__ == "__main__":
result = swarm.run("build a research pipeline")
print(result)
Implementation plan
1. Config dict → Agent() parameter mapping (swarms/agents/auto_generate_swarm_config.py)
Add a config_dict_to_agent_code(agent_dict: dict) -> str helper that maps the parsed YAML fields to the Agent(...) constructor signature:
| YAML key |
Agent() param |
agent_name |
agent_name |
system_prompt |
system_prompt |
description |
agent_description |
model_name |
model_name |
max_loops |
max_loops |
temperature |
temperature |
max_tokens |
max_tokens |
autosave |
autosave |
verbose |
verbose |
output_type |
output_type |
context_length |
context_length |
Unknown keys should be emitted as **kwargs-style comments so the user knows they may need to map them manually.
2. File writer (swarms/agents/auto_generate_swarm_config.py or new swarms/utils/swarm_file_writer.py)
def write_autoswarm_file(
config: dict,
output_path: str = "autoswarm_output.py",
) -> str:
"""
Given a parsed swarm config dict, render a ready-to-run Python file
and write it to `output_path`. Returns the resolved file path.
"""
- Slugify the swarm
name field to build the default filename (e.g. Research Pipeline → autoswarm_research_pipeline.py)
- Include a header comment noting the file was auto-generated
- Import only what is needed (
Agent, the relevant swarm struct)
- Emit a
if __name__ == "__main__": block with swarm.run(task)
3. Wire into run_autoswarm (swarms/cli/main.py:55)
result = generate_swarm_config(task=task, model=model)
output_file = write_autoswarm_file(config=result, task=task)
console.print(f"[green]✓ Written to: {output_file}[/green]")
Add an optional --output / -o flag to setup_argument_parser() so users can specify a custom output path.
4. Preserve existing run-immediately behaviour
Keep the existing in-process execution path. Add a --no-run flag that skips execution and only writes the file, for users who just want the generated script.
Relevant files
| File |
Role |
swarms/cli/main.py:55 |
run_autoswarm() — entry point to modify |
swarms/agents/auto_generate_swarm_config.py |
generate_swarm_config() — produces the config dict |
swarms/agents/create_agents_from_yaml.py |
create_agents_from_yaml() — existing YAML→Agent pipeline for reference |
swarms/structs/auto_swarm_builder.py |
AgentSpec, SwarmRouterConfig Pydantic models — use for field validation |
swarms/structs/agent.py |
Agent.__init__ — canonical parameter list |
swarms/cli/utils.py |
CLI helpers, console, error formatting |
Acceptance criteria
Labels
enhancement, cli, autoswarm, good first issue
Summary
The
autoswarmCLI command currently callsgenerate_swarm_config()which produces a YAML config and runs the swarm internally, but never persists a usable Python file for the user. The user has no artifact they can inspect, modify, or re-run without touching the CLI again.This issue tracks adding a full pipeline to
autoswarmthat:generate_swarm_config/AutoSwarmBuilder)Agent()structs — map every key in the config dict to the correctAgent(...)constructor parameter.pyfile in the current working directory so the user has a reusable, editable scriptCurrent behavior (
swarms/cli/main.py:55–100,swarms/agents/auto_generate_swarm_config.py)generate_swarm_config(task=task, model=model)create_agents_from_yaml(), swarm runsDesired behavior
Expected output:
Generated file (
autoswarm_research_pipeline.py):Implementation plan
1. Config dict →
Agent()parameter mapping (swarms/agents/auto_generate_swarm_config.py)Add a
config_dict_to_agent_code(agent_dict: dict) -> strhelper that maps the parsed YAML fields to theAgent(...)constructor signature:Agent()paramagent_nameagent_namesystem_promptsystem_promptdescriptionagent_descriptionmodel_namemodel_namemax_loopsmax_loopstemperaturetemperaturemax_tokensmax_tokensautosaveautosaveverboseverboseoutput_typeoutput_typecontext_lengthcontext_lengthUnknown keys should be emitted as
**kwargs-style comments so the user knows they may need to map them manually.2. File writer (
swarms/agents/auto_generate_swarm_config.pyor newswarms/utils/swarm_file_writer.py)namefield to build the default filename (e.g.Research Pipeline→autoswarm_research_pipeline.py)Agent, the relevant swarm struct)if __name__ == "__main__":block withswarm.run(task)3. Wire into
run_autoswarm(swarms/cli/main.py:55)Add an optional
--output/-oflag tosetup_argument_parser()so users can specify a custom output path.4. Preserve existing run-immediately behaviour
Keep the existing in-process execution path. Add a
--no-runflag that skips execution and only writes the file, for users who just want the generated script.Relevant files
swarms/cli/main.py:55run_autoswarm()— entry point to modifyswarms/agents/auto_generate_swarm_config.pygenerate_swarm_config()— produces the config dictswarms/agents/create_agents_from_yaml.pycreate_agents_from_yaml()— existing YAML→Agent pipeline for referenceswarms/structs/auto_swarm_builder.pyAgentSpec,SwarmRouterConfigPydantic models — use for field validationswarms/structs/agent.pyAgent.__init__— canonical parameter listswarms/cli/utils.pyAcceptance criteria
swarms autoswarm --task "..." --model "..."writes a.pyfile to the current directoryAgent(...)variable in the outputSwarmRouter,SequentialWorkflow, etc.)--output/-oflag lets the user override the output path--no-runflag skips in-process execution and only writes the fileLabels
enhancement,cli,autoswarm,good first issue