Skip to content

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

.

sid-muralid010b0f3d457
diff --git a/deepteam/guardrails/api.py b/deepteam/guardrails/api.py
index 4660a28c..ec1c7383 100644
--- a/deepteam/guardrails/api.py
+++ b/deepteam/guardrails/api.py
@@ -15,7 +15,7 @@ class GuardData(BaseModel):
guard: str
score: float
reason: str
- safe: bool = True
+ safe: bool
class GuardResult(BaseModel):
@@ -23,4 +23,4 @@ class GuardResult(BaseModel):
Result from guardrails evaluation.
"""
breached: bool
- guard_results: Dict[str, GuardData]
+ guard_verdicts: Dict[str, GuardData]
diff --git a/deepteam/guardrails/base_guard.py b/deepteam/guardrails/base_guard.py
index e46a28ab..6c514d5e 100644
--- a/deepteam/guardrails/base_guard.py
+++ b/deepteam/guardrails/base_guard.py
@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
from typing import Optional, Dict
from deepeval.models import GPTModel
+from deepeval.models.llms.utils import trim_and_load_json
import json
from deepteam.guardrails.types import GuardType
-class BaseDecorativeGuard(ABC):
+class BaseGuard(ABC):
score: Optional[float] = None
- score_breakdown: Dict = None
reason: Optional[str] = None
evaluation_model: Optional[str] = None
error: Optional[str] = None
@@ -15,52 +15,52 @@ class BaseDecorativeGuard(ABC):
def __init__(self):
self.model = GPTModel(model="gpt-4.1-2025-04-14")
- self.evaluation_model = "gpt-4.1-2025-04-14"
+ self.evaluation_model = self.model.get_model_name()
@property
def __name__(self):
- return "Base Decorative Guard"
+ return "Base Guard"
- def guard(self, input: str = None, response: str = None, **kwargs) -> float:
+ def guard(self, input: str = None, output: str = None, **kwargs) -> float:
"""
Guard method - returns score between 0.0 (unsafe) and 1.0 (safe)
"""
try:
- if response is not None:
- prompt = self._get_output_prompt(input or "", response)
+ if output is not None:
+ prompt = self._get_output_prompt(input or "", output)
else:
prompt = self._get_input_prompt(input)
- llm_response = self.model.generate(prompt)
+ llm_output = self.model.generate(prompt)
# Handle tuple response
- if isinstance(llm_response, tuple):
- llm_response = llm_response[0]
+ if isinstance(llm_output, tuple):
+ llm_output = llm_output[0]
- return self._parse_json_response(llm_response)
+ return self._parse_json_response(llm_output)
except Exception as e:
self.error = str(e)
self.score = 0.0
return 0.0
- async def a_guard(self, input: str = None, response: str = None, **kwargs) -> float:
+ async def a_guard(self, input: str = None, output: str = None, **kwargs) -> float:
"""
Async guard method - returns score between 0.0 (unsafe) and 1.0 (safe)
"""
try:
- if response is not None:
- prompt = self._get_output_prompt(input or "", response)
+ if output is not None:
+ prompt = self._get_output_prompt(input or "", output)
else:
prompt = self._get_input_prompt(input)
- llm_response = await self.model.a_generate(prompt)
+ llm_output = await self.model.a_generate(prompt)
# Handle tuple response
- if isinstance(llm_response, tuple):
- llm_response = llm_response[0]
+ if isinstance(llm_output, tuple):
+ llm_output = llm_output[0]
- return self._parse_json_response(llm_response)
+ return self._parse_json_response(llm_output)
except Exception as e:
self.error = str(e)
@@ -73,52 +73,13 @@ class BaseDecorativeGuard(ABC):
Returns 1.0 if safe, 0.0 if unsafe
"""
try:
- response_clean = response.strip()
-
- start_idx = response_clean.find('{')
- end_idx = response_clean.rfind('}')
-
- if start_idx != -1 and end_idx != -1 and end_idx > start_idx:
- json_str = response_clean[start_idx:end_idx + 1]
- data = json.loads(json_str)
- else:
- data = json.loads(response_clean)
-
- is_safe = False
- if "safe" in data:
- is_safe = bool(data["safe"])
- elif "is_safe" in data:
- is_safe = bool(data["is_safe"])
- elif "verdict" in data:
- verdict = str(data["verdict"]).lower()
- is_safe = verdict in ["safe", "true", "yes"]
-
- self.reason = data.get("reason", data.get("explanation", "No explanation provided"))
+ data = trim_and_load_json(response)
+ is_safe = data.get("safe", False)
+ self.reason = data.get("reason", "No explanation provided")
self.score = 1.0 if is_safe else 0.0
-
return self.score
-
- except (json.JSONDecodeError, KeyError, ValueError) as e:
-
- response_lower = response.lower().strip()
-
- unsafe_patterns = ["unsafe", "not safe", "false", "dangerous", "harmful", "violation", "breach"]
- safe_patterns = ["safe", "true", "secure", "acceptable", "compliant"]
-
- for pattern in unsafe_patterns:
- if pattern in response_lower:
- self.reason = f"Detected as unsafe via fallback parsing (found: {pattern})"
- self.score = 0.0
- return 0.0
-
- for pattern in safe_patterns:
- if pattern in response_lower:
- self.reason = f"Detected as safe via fallback parsing (found: {pattern})"
- self.score = 1.0
- return 1.0
-
- # Default to unsafe if completely unclear
- self.reason = f"Unclear response, defaulting to unsafe. Response: {response[:100]}..."
+ except Exception as e:
+ self.error = str(e)
self.score = 0.0
return 0.0
@@ -136,8 +97,6 @@ class BaseDecorativeGuard(ABC):
"""
pass
-
-class BaseGuard(BaseDecorativeGuard):
@abstractmethod
async def a_guard_input(self, input: str, *args, **kwargs) -> float:
raise NotImplementedError(
@@ -151,7 +110,3 @@ class BaseGuard(BaseDecorativeGuard):
raise NotImplementedError(
f"Async execution for {self.__class__.__name__} not supported yet."
)
-
- @property
- def __name__(self):
- return "Base Guard"
diff --git a/deepteam/guardrails/cybersecurity_guard/cybersecurity_guard.py b/deepteam/guardrails/cybersecurity_guard/cybersecurity_guard.py
index a1697566..22c21e8f 100644
--- a/deepteam/guardrails/cybersecurity_guard/cybersecurity_guard.py
+++ b/deepteam/guardrails/cybersecurity_guard/cybersecurity_guard.py
@@ -1,18 +1,19 @@
-from typing import List
+from typing import List, Optional
from .category import CyberattackCategory
-from ..base_guard import BaseDecorativeGuard
+from ..base_guard import BaseGuard
from ..types import GuardType
-class CybersecurityGuard(BaseDecorativeGuard):
+class CybersecurityGuard(BaseGuard):
def __init__(
self,
- purpose: str = "general application security",
+ purpose: Optional[str] = None,
categories: List[CyberattackCategory] = None,
+ guard_type: GuardType = GuardType.INPUT,
):
super().__init__()
- self.guard_type = GuardType.INPUT
+ self.guard_type = guard_type
self.purpose = purpose
self.categories = categories or [attack for attack in CyberattackCategory]
@@ -22,10 +23,11 @@ class CybersecurityGuard(BaseDecorativeGuard):
def _get_input_prompt(self, input_text: str) -> str:
categories_text = ", ".join([cat.value for cat in self.categories])
+ purpose_line = f"Application purpose: {self.purpose}\n" if self.purpose else ""
+
return f"""You are a cybersecurity expert. Determine if the input contains potential cybersecurity attacks or malicious content.
-Application purpose: {self.purpose}
-Categories to check: {categories_text}
+{purpose_line}Categories to check: {categories_text}
Analyze the input for:
1. BFLA (Broken Function Level Authorization) attempts
@@ -45,10 +47,11 @@ Input to analyze: {input_text}"""
def _get_output_prompt(self, input_text: str, output_text: str) -> str:
categories_text = ", ".join([cat.value for cat in self.categories])
+ purpose_line = f"Application purpose: {self.purpose}\n" if self.purpose else ""
+
return f"""You are a cybersecurity expert. Analyze if this output contains security vulnerabilities.
-Application purpose: {self.purpose}
-Categories to check: {categories_text}
+{purpose_line}Categories to check: {categories_text}
Respond in JSON format: {{"safe": true/false, "reason": "explanation"}}
diff --git a/deepteam/guardrails/guardrails.py b/deepteam/guardrails/guardrails.py
index 928d27a2..7c2f6e4d 100644
--- a/deepteam/guardrails/guardrails.py
+++ b/deepteam/guardrails/guardrails.py
@@ -7,9 +7,13 @@ from deepteam.guardrails.types import GuardType
class GuardResult:
- def __init__(self, breached: bool, guard_results: Dict[str, Any]):
- self.breached = breached
+ def __init__(self, guard_results: Dict[str, Any]):
self.guard_results = guard_results
+ # Calculate breached automatically from guard results
+ self.breached = any(
+ not result.get("safe", False)
+ for result in guard_results.values()
+ )
class Guardrails:
@@ -32,7 +36,6 @@ class Guardrails:
)
guard_results = {}
- any_breached = False
for guard in self.guards:
start_time = time.time()
@@ -46,9 +49,6 @@ class Guardrails:
"reason": getattr(guard, "reason", None),
"score": getattr(guard, "score", None)
}
-
- if not is_safe:
- any_breached = True
except Exception as e:
guard_results[guard.__name__] = {
@@ -56,9 +56,8 @@ class Guardrails:
"latency": time.time() - start_time,
"error": str(e)
}
- any_breached = True
- return GuardResult(breached=any_breached, guard_results=guard_results)
+ return GuardResult(guard_results=guard_results)
def guard_output(self, input: str, output: str) -> GuardResult:
"""
@@ -71,7 +70,6 @@ class Guardrails:
)
guard_results = {}
- any_breached = False
for guard in self.guards:
start_time = time.time()
@@ -85,9 +83,6 @@ class Guardrails:
"reason": getattr(guard, "reason", None),
"score": getattr(guard, "score", None)
}
-
- if not is_safe:
- any_breached = True
except Exception as e:
guard_results[guard.__name__] = {
@@ -95,9 +90,8 @@ class Guardrails:
"latency": time.time() - start_time,
"error": str(e)
}
- any_breached = True
- return GuardResult(breached=any_breached, guard_results=guard_results)
+ return GuardResult(guard_results=guard_results)
async def a_guard_input(self, input: str) -> GuardResult:
"""
@@ -114,22 +108,18 @@ class Guardrails:
tasks.append((guard, task))
guard_results = {}
- any_breached = False
for guard, task in tasks:
try:
result = await task
guard_results[guard.__name__] = result
- if not result["safe"]:
- any_breached = True
except Exception as e:
guard_results[guard.__name__] = {
"safe": False,
"error": str(e)
}
- any_breached = True
- return GuardResult(breached=any_breached, guard_results=guard_results)
+ return GuardResult(guard_results=guard_results)
async def a_guard_output(self, input: str, output: str) -> GuardResult:
"""
@@ -146,22 +136,18 @@ class Guardrails:
tasks.append((guard, task))
guard_results = {}
- any_breached = False
for guard, task in tasks:
try:
result = await task
guard_results[guard.__name__] = result
- if not result["safe"]:
- any_breached = True
except Exception as e:
guard_results[guard.__name__] = {
"safe": False,
"error": str(e)
}
- any_breached = True
- return GuardResult(breached=any_breached, guard_results=guard_results)
+ return GuardResult(guard_results=guard_results)
async def _async_guard_input_single(self, guard: BaseGuard, input: str):
"""Helper method for async input guarding."""