From d817ee791361014dd5ba1ed8061a06cbf5eecfee Mon Sep 17 00:00:00 2001 From: Steve-Dusty Date: Wed, 19 Nov 2025 19:05:03 -0800 Subject: [PATCH 1/3] added run_async to agent rearrange and handeled test sequantil workflow init --- swarms/structs/agent_rearrange.py | 40 +++++++++++++++++++++++ tests/structs/test_sequential_workflow.py | 14 ++------ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/swarms/structs/agent_rearrange.py b/swarms/structs/agent_rearrange.py index d3016de4..c962a518 100644 --- a/swarms/structs/agent_rearrange.py +++ b/swarms/structs/agent_rearrange.py @@ -908,6 +908,46 @@ class AgentRearrange: except Exception as e: self._catch_error(e) + async def run_async( + self, + task: str, + img: Optional[str] = None, + *args, + **kwargs, + ) -> Any: + """ + Asynchronously executes a task through the agent workflow. + + This method enables asynchronous execution of tasks by running the + synchronous run method in a separate thread using asyncio.to_thread. + This is ideal for integrating the agent workflow into async applications + or when you want non-blocking execution. + + Args: + task (str): The task to be executed through the agent workflow. + img (Optional[str]): Optional image input for the task. Defaults to None. + *args: Additional positional arguments passed to the run method. + **kwargs: Additional keyword arguments passed to the run method. + + Returns: + Any: The result of the task execution, format depends on output_type setting. + + Raises: + Exception: If an error occurs during task execution. + + Note: + This method uses asyncio.to_thread to run the synchronous run method + asynchronously, allowing integration with async/await patterns. + """ + import asyncio + + try: + return await asyncio.to_thread( + self.run, task=task, img=img, *args, **kwargs + ) + except Exception as e: + self._catch_error(e) + def _serialize_callable( self, attr_value: Callable ) -> Dict[str, Any]: diff --git a/tests/structs/test_sequential_workflow.py b/tests/structs/test_sequential_workflow.py index 6d8f74a1..f905fe47 100644 --- a/tests/structs/test_sequential_workflow.py +++ b/tests/structs/test_sequential_workflow.py @@ -5,17 +5,9 @@ from swarms import Agent, SequentialWorkflow # Test SequentialWorkflow class def test_sequential_workflow_initialization(): - workflow = SequentialWorkflow() - assert isinstance(workflow, SequentialWorkflow) - assert len(workflow.tasks) == 0 - assert workflow.max_loops == 1 - assert workflow.autosave is False - assert ( - workflow.saved_state_filepath - == "sequential_workflow_state.json" - ) - assert workflow.restore_state_filepath is None - assert workflow.dashboard is False + # SequentialWorkflow requires agents, so expect ValueError + with pytest.raises(ValueError, match="Agents list cannot be None or empty"): + workflow = SequentialWorkflow() def test_sequential_workflow_initialization_with_agents(): From 4635903c84227334f1db0f427519b430879b451c Mon Sep 17 00:00:00 2001 From: Steve-Dusty Date: Wed, 19 Nov 2025 19:19:18 -0800 Subject: [PATCH 2/3] added import asyncio to the top --- swarms/structs/agent_rearrange.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/swarms/structs/agent_rearrange.py b/swarms/structs/agent_rearrange.py index c962a518..a0155ef6 100644 --- a/swarms/structs/agent_rearrange.py +++ b/swarms/structs/agent_rearrange.py @@ -1,7 +1,7 @@ import json from concurrent.futures import ThreadPoolExecutor from typing import Any, Callable, Dict, List, Optional, Union - +import asyncio from swarms.structs.agent import Agent from swarms.structs.conversation import Conversation from swarms.structs.multi_agent_exec import run_agents_concurrently @@ -939,7 +939,6 @@ class AgentRearrange: This method uses asyncio.to_thread to run the synchronous run method asynchronously, allowing integration with async/await patterns. """ - import asyncio try: return await asyncio.to_thread( From a59e8c8dc0b9f5bd8ef0bbbfba251c29440543d2 Mon Sep 17 00:00:00 2001 From: Steve-Dusty Date: Wed, 19 Nov 2025 21:28:21 -0800 Subject: [PATCH 3/3] removed useless tests --- tests/structs/test_sequential_workflow.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/structs/test_sequential_workflow.py b/tests/structs/test_sequential_workflow.py index f905fe47..e4a48a20 100644 --- a/tests/structs/test_sequential_workflow.py +++ b/tests/structs/test_sequential_workflow.py @@ -3,12 +3,6 @@ import pytest from swarms import Agent, SequentialWorkflow -# Test SequentialWorkflow class -def test_sequential_workflow_initialization(): - # SequentialWorkflow requires agents, so expect ValueError - with pytest.raises(ValueError, match="Agents list cannot be None or empty"): - workflow = SequentialWorkflow() - def test_sequential_workflow_initialization_with_agents(): """Test SequentialWorkflow initialization with agents"""