Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Enh(agent/models): Attempt to work around policy violation errors

Kilian Lieret190a6e9fb8b2
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 6e9f245c..0c377a66 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -30,7 +30,12 @@ from sweagent.agent.models import (
from sweagent.agent.problem_statement import ProblemStatement, ProblemStatementConfig
from sweagent.agent.reviewer import AbstractReviewLoop, ReviewLoopConfig, get_review_loop_from_config
from sweagent.environment.swe_env import SWEEnv
-from sweagent.exceptions import ContextWindowExceededError, CostLimitExceededError, FormatError
+from sweagent.exceptions import (
+ ContentPolicyViolationError,
+ ContextWindowExceededError,
+ CostLimitExceededError,
+ FormatError,
+)
from sweagent.tools.parsing import (
ActionOnlyParser,
ThoughtActionParser,
@@ -852,6 +857,11 @@ class Agent:
history = handle_error_with_retry(
exception=e, template=self.tools.config.filter.blocklist_error_template, n_requeries=n_format_fails
)
+ except ContentPolicyViolationError:
+ self.logger.warning("Content policy violation, trying to resample")
+ n_format_fails += 1
+ # Try if simply resampling helps here
+ pass
except BashIncorrectSyntaxError as e:
n_format_fails += 1
history = handle_error_with_retry(
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 47cc2a37..ec4b5d28 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -25,6 +25,7 @@ from tenacity import (
from sweagent import REPO_ROOT
from sweagent.exceptions import (
+ ContentPolicyViolationError,
ContextWindowExceededError,
CostLimitExceededError,
FunctionCallingFormatError,
@@ -575,6 +576,8 @@ class LiteLLMModel(AbstractModel):
)
except litellm.exceptions.ContextWindowExceededError as e:
raise ContextWindowExceededError from e
+ except litellm.exceptions.ContentPolicyViolationError as e:
+ raise ContentPolicyViolationError from e
self.logger.info(f"Response: {response}")
cost = litellm.cost_calculator.completion_cost(response)
choices: litellm.types.utils.Choices = response.choices # type: ignore
@@ -624,6 +627,7 @@ class LiteLLMModel(AbstractModel):
litellm.exceptions.ContentPolicyViolationError,
TypeError,
litellm.exceptions.AuthenticationError,
+ ContentPolicyViolationError,
)
),
):
diff --git a/sweagent/exceptions.py b/sweagent/exceptions.py
index 800f6213..f430f574 100644
--- a/sweagent/exceptions.py
+++ b/sweagent/exceptions.py
@@ -42,3 +42,7 @@ class TotalCostLimitExceededError(CostLimitExceededError):
class AttemptCostLimitExceededError(CostLimitExceededError):
"""Raised when we exceed the cost limit for an attempt"""
+
+
+class ContentPolicyViolationError(Exception):
+ """Raised when the model response violates a content policy"""