Skip to content

DeepTeam is a framework to red team LLMs and AI agents.

fix retry env var

A-Vamshi9717604d612e
diff --git a/deepteam/attacks/attack_simulator/utils.py b/deepteam/attacks/attack_simulator/utils.py
index 0e546a28..88b64fe1 100644
--- a/deepteam/attacks/attack_simulator/utils.py
+++ b/deepteam/attacks/attack_simulator/utils.py
@@ -13,7 +13,14 @@ from deepteam.attacks.single_turn.escalation_constants import (
random_escalation_suffix,
)
-MAX_RETRIES = os.getenv("DEEPTEAM_MAX_RETRIES", 3)
+def _max_retries() -> int:
+ max_retries = os.getenv("DEEPTEAM_MAX_RETRIES")
+ if max_retries is None:
+ return 3
+ try:
+ return max(1, int(max_retries))
+ except ValueError:
+ return 3
def add_cost(*costs):
@@ -79,8 +86,9 @@ def generate_with_cost(
_, using_native_model = initialize_model(model=model)
last_error = None
retry_prompt = prompt
+ max_retries = _max_retries()
- for attempt in range(MAX_RETRIES):
+ for attempt in range(max_retries):
try:
if using_native_model:
res, cost = model.generate(prompt=retry_prompt, schema=schema)
@@ -120,7 +128,7 @@ def generate_with_cost(
except Exception as e:
last_error = e
- if attempt < MAX_RETRIES - 1:
+ if attempt < max_retries - 1:
escalated_prompt = (
f"{random_escalation_suffix(attempt)} \n\n {prompt}"
)
@@ -132,7 +140,7 @@ def generate_with_cost(
time.sleep(sleep_time)
raise RuntimeError(
- f"Failed to generate after {MAX_RETRIES} attempts. Last error: {last_error}"
+ f"Failed to generate after {max_retries} attempts. Last error: {last_error}"
)
@@ -171,8 +179,9 @@ async def a_generate_with_cost(
_, using_native_model = initialize_model(model=model)
last_error = None
retry_prompt = prompt
+ max_retries = _max_retries()
- for attempt in range(MAX_RETRIES):
+ for attempt in range(max_retries):
try:
if using_native_model:
res, cost = await model.a_generate(
@@ -216,7 +225,7 @@ async def a_generate_with_cost(
except Exception as e:
last_error = e
- if attempt < MAX_RETRIES - 1:
+ if attempt < max_retries - 1:
escalated_prompt = (
f"{random_escalation_suffix(attempt)} \n\n {prompt}"
)
@@ -228,7 +237,7 @@ async def a_generate_with_cost(
await asyncio.sleep(sleep_time)
raise RuntimeError(
- f"Failed to async generate after {MAX_RETRIES} attempts. Last error: {last_error}"
+ f"Failed to async generate after {max_retries} attempts. Last error: {last_error}"
)