Skip to content

Commit c5aeebc

Browse files
committed
feat: Add tool retries and stop-on-failure logic to Python e2e tests
1 parent 58f8380 commit c5aeebc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
from strands.hooks import HookProvider, HookRegistry, AfterToolCallEvent
3+
4+
5+
class FailOnToolError(HookProvider):
6+
def __init__(self, max_retries: int = 3, retry_delay: float = 1.0):
7+
self.max_retries = max_retries
8+
self.retry_delay = retry_delay
9+
10+
def register_hooks(self, registry: HookRegistry) -> None:
11+
registry.add_callback(AfterToolCallEvent, self.check_result)
12+
13+
def check_result(self, event: AfterToolCallEvent) -> None:
14+
if event.result.get("isError", False):
15+
for attempt in range(self.max_retries):
16+
time.sleep(self.retry_delay)
17+
try:
18+
# Retry the tool execution
19+
result = event.selected_tool(event.tool_use, event.invocation_state)
20+
if not result.get("isError", False):
21+
event.result = result
22+
return
23+
except Exception:
24+
continue
25+
26+
# All retries failed
27+
error_content = event.result.get("content", [{}])[0].get("text", "Unknown error")
28+
raise RuntimeError(f"Tool {event.tool_use['name']} failed after {self.max_retries} retries: {error_content}")

e2e_tests/python/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
create_lambda_function_url_client,
1313
create_automated_oauth_client,
1414
)
15+
from fail_on_tool_error import FailOnToolError
1516

1617
# Configure logging
1718
logging.basicConfig(
@@ -83,6 +84,7 @@ def main() -> None:
8384
model=bedrock_model,
8485
tools=mcp_clients,
8586
system_prompt="You are a helpful assistant.",
87+
hooks=[FailOnToolError()],
8688
)
8789

8890
# Run test utterances

0 commit comments

Comments
 (0)