Skip to content

Commit 4dfa9eb

Browse files
committed
[bugf][agent][make tool_execution_retry actually retry and surface the failure]
1 parent 16afc75 commit 4dfa9eb

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

swarms/structs/agent.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5548,20 +5548,26 @@ def tool_execution_retry(self, response: any, loop_count: int):
55485548
>>> agent.tool_execution_retry(None, loop_count=2)
55495549
>>> # Logs warning but does not raise exception
55505550
"""
5551-
try:
5552-
if response is not None:
5551+
if response is None:
5552+
logger.warning(
5553+
f"Agent '{self.agent_name}' received None response from LLM in loop {loop_count}. "
5554+
f"This may indicate an issue with the model or prompt. Skipping tool execution."
5555+
)
5556+
return
5557+
5558+
attempts = max(1, self.tool_retry_attempts or 1)
5559+
for attempt in range(1, attempts + 1):
5560+
try:
55535561
self.execute_tools(
55545562
response=response,
55555563
loop_count=loop_count,
55565564
)
5557-
else:
5558-
logger.warning(
5559-
f"Agent '{self.agent_name}' received None response from LLM in loop {loop_count}. "
5560-
f"This may indicate an issue with the model or prompt. Skipping tool execution."
5565+
return
5566+
except AgentToolExecutionError as e:
5567+
logger.error(
5568+
f"Agent '{self.agent_name}' encountered error during tool execution in loop {loop_count}: {str(e)}. "
5569+
f"Full traceback: {traceback.format_exc()}. "
5570+
f"Attempt {attempt} of {attempts}"
55615571
)
5562-
except AgentToolExecutionError as e:
5563-
logger.error(
5564-
f"Agent '{self.agent_name}' encountered error during tool execution in loop {loop_count}: {str(e)}. "
5565-
f"Full traceback: {traceback.format_exc()}. "
5566-
f"Attempting to retry tool execution with 3 attempts"
5567-
)
5572+
if attempt == attempts:
5573+
raise

tests/structs/test_agent.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Agent,
2121
create_agents_from_yaml,
2222
)
23+
from swarms.schemas.agent_errors import AgentToolExecutionError
2324

2425
# Load environment variables
2526
load_dotenv()
@@ -2553,6 +2554,39 @@ def test_default_is_per_instance(self):
25532554
)
25542555

25552556

2557+
# ============================================================================
2558+
# TOOL EXECUTION RETRY
2559+
# ============================================================================
2560+
2561+
2562+
class TestToolExecutionRetry:
2563+
"""tool_execution_retry must actually retry, then surface the failure."""
2564+
2565+
def test_retries_then_raises(self):
2566+
with patch("swarms.structs.agent.LiteLLM"):
2567+
agent = Agent(
2568+
agent_name="retry_agent",
2569+
model_name="gpt-5.4",
2570+
max_loops=1,
2571+
print_on=False,
2572+
verbose=False,
2573+
persistent_memory=False,
2574+
tool_retry_attempts=3,
2575+
)
2576+
2577+
with patch.object(
2578+
agent,
2579+
"execute_tools",
2580+
side_effect=AgentToolExecutionError("tool blew up"),
2581+
) as execute:
2582+
with pytest.raises(AgentToolExecutionError):
2583+
agent.tool_execution_retry(
2584+
[{"function": {"name": "x"}}], loop_count=1
2585+
)
2586+
2587+
assert execute.call_count == 3
2588+
2589+
25562590
# ============================================================================
25572591
# MAIN TEST RUNNER
25582592
# ============================================================================

0 commit comments

Comments
 (0)