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]

Ref: Move all exceptions to one custom file

Kilian Lieret92943e9a110b
diff --git a/docs/reference/exceptions.md b/docs/reference/exceptions.md
new file mode 100644
index 00000000..1f226bfb
--- /dev/null
+++ b/docs/reference/exceptions.md
@@ -0,0 +1,6 @@
+# Custom exceptions raised by SWE-agent
+
+::: sweagent.exceptions
+ options:
+ show_root_full_path: false
+ show_bases: false
diff --git a/mkdocs.yml b/mkdocs.yml
index 162154f9..c4da5bef 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -79,6 +79,7 @@ nav:
- "Environment config": "reference/env_config.md"
- "Agent class": "reference/agent.md"
- "Environment class": "reference/env.md"
+ - "Exceptions": "reference/exceptions.md"
plugins:
- glightbox
- search
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 7770f364..f950be55 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -20,8 +20,6 @@ from sweagent.agent.history_processors import DefaultHistoryProcessor, HistoryPr
from sweagent.agent.hooks.abstract import AbstractAgentHook, CombinedAgentHook
from sweagent.agent.models import (
AbstractModel,
- ContextWindowExceededError,
- CostLimitExceededError,
HumanModel,
HumanThoughtModel,
InstanceStats,
@@ -30,8 +28,8 @@ from sweagent.agent.models import (
)
from sweagent.environment.config.problem_statement import ProblemStatement, ProblemStatementConfig
from sweagent.environment.swe_env import SWEEnv
+from sweagent.exceptions import ContextWindowExceededError, CostLimitExceededError, FormatError
from sweagent.tools.parsing import (
- FormatError,
ThoughtActionParser,
)
from sweagent.tools.tools import ToolConfig, ToolHandler
@@ -122,20 +120,14 @@ class AgentConfig(BaseModel):
model_config = ConfigDict(extra="forbid")
-class BlockedActionError(Exception):
+class _BlockedActionError(Exception):
"""Raised when the agent's action is blocked"""
-class Skipped(Exception): ...
+class _RetryWithOutput(Exception): ...
-class Submitted(Exception): ...
-
-
-class RetryWithOutput(Exception): ...
-
-
-class RetryWithoutOutput(Exception): ...
+class _RetryWithoutOutput(Exception): ...
RETRY_WITH_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITH-OUTPUT###"
@@ -596,7 +588,7 @@ class Agent:
action_execution_output: action execution output
"""
if self.tools.should_block_action(step.action):
- raise BlockedActionError()
+ raise _BlockedActionError()
if step.action.strip() == "exit":
self.logger.info("Exiting agent")
@@ -636,10 +628,10 @@ class Agent:
if RETRY_WITH_OUTPUT_TOKEN in step.observation:
step.observation = step.observation.replace(RETRY_WITH_OUTPUT_TOKEN, "")
- raise RetryWithOutput()
+ raise _RetryWithOutput()
elif RETRY_WITHOUT_OUTPUT_TOKEN in step.observation:
step.observation = step.observation.replace(RETRY_WITHOUT_OUTPUT_TOKEN, "")
- raise RetryWithoutOutput()
+ raise _RetryWithoutOutput()
return self.handle_submission(step)
@@ -743,7 +735,7 @@ class Agent:
except FormatError as e:
n_format_fails += 1
history = handle_error_with_retry(exception=e, template=self.tools.config.format_error_template)
- except BlockedActionError as e:
+ except _BlockedActionError as e:
n_format_fails += 1
history = handle_error_with_retry(
exception=e, template=self.tools.config.filter.blocklist_error_template
@@ -754,13 +746,13 @@ class Agent:
exception=e,
template=self.templates.shell_check_error_template,
)
- except RetryWithOutput as e:
+ except _RetryWithOutput as e:
n_format_fails = 0
history = handle_error_with_retry(
exception=e,
template=self.templates.next_step_template,
)
- except RetryWithoutOutput:
+ except _RetryWithoutOutput:
n_format_fails = 0
# Requery with the same template as the last step
diff --git a/sweagent/agent/history_processors.py b/sweagent/agent/history_processors.py
index b82ea6f4..d3040015 100644
--- a/sweagent/agent/history_processors.py
+++ b/sweagent/agent/history_processors.py
@@ -9,10 +9,6 @@ from pydantic import BaseModel, ConfigDict, Field
from sweagent.types import History, HistoryItem
-class FormatError(Exception):
- pass
-
-
class AbstractHistoryProcessor(Protocol):
@abstractmethod
def __call__(self, history: History) -> History:
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 646f13c3..09d73c4c 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -22,7 +22,13 @@ from tenacity import (
)
from sweagent import REPO_ROOT
-from sweagent.tools.parsing import FunctionCallingFormatError
+from sweagent.exceptions import (
+ ContextWindowExceededError,
+ CostLimitExceededError,
+ FunctionCallingFormatError,
+ InstanceCostLimitExceededError,
+ TotalCostLimitExceededError,
+)
from sweagent.tools.tools import ToolConfig
from sweagent.types import History, HistoryItem
from sweagent.utils.log import get_logger
@@ -184,22 +190,6 @@ class InstanceStats(PydanticBaseModel):
)
-class ContextWindowExceededError(Exception):
- """Raised when the context window of a LM is exceeded"""
-
-
-class CostLimitExceededError(Exception):
- """Raised when we exceed a cost limit"""
-
-
-class InstanceCostLimitExceededError(CostLimitExceededError):
- """Raised when we exceed the cost limit set for one task instance"""
-
-
-class TotalCostLimitExceededError(CostLimitExceededError):
- """Raised when we exceed the total cost limit"""
-
-
class AbstractModel(ABC):
def __init__(self, config: PydanticBaseModel, tools: ToolConfig):
self.config: PydanticBaseModel
diff --git a/sweagent/exceptions.py b/sweagent/exceptions.py
new file mode 100644
index 00000000..c1da7681
--- /dev/null
+++ b/sweagent/exceptions.py
@@ -0,0 +1,40 @@
+from typing import Any, Literal
+
+"""This module contains all custom exceptions used by the SWE-agent."""
+
+
+class FormatError(Exception):
+ """Raised when the model response cannot properly be parsed into thought and actions."""
+
+
+class FunctionCallingFormatError(FormatError):
+ """Format error exception used by the function
+ calling parser."""
+
+ def __init__(
+ self,
+ message: str,
+ error_code: Literal[
+ "missing", "multiple", "incorrect_args", "invalid_json", "invalid_command", "missing_arg", "unexpected_arg"
+ ],
+ **extra_info: Any,
+ ):
+ super().__init__(message + f" [error_code={error_code}]")
+ self.message = message
+ self.extra_info = {"error_code": error_code, **extra_info}
+
+
+class ContextWindowExceededError(Exception):
+ """Raised when the context window of a LM is exceeded"""
+
+
+class CostLimitExceededError(Exception):
+ """Raised when we exceed a cost limit"""
+
+
+class InstanceCostLimitExceededError(CostLimitExceededError):
+ """Raised when we exceed the cost limit set for one task instance"""
+
+
+class TotalCostLimitExceededError(CostLimitExceededError):
+ """Raised when we exceed the total cost limit"""
diff --git a/sweagent/tools/parsing.py b/sweagent/tools/parsing.py
index c08df4d6..d24a4439 100644
--- a/sweagent/tools/parsing.py
+++ b/sweagent/tools/parsing.py
@@ -6,36 +6,16 @@ import textwrap
from abc import ABC, abstractmethod
from shlex import quote
from textwrap import dedent
-from typing import Any, Literal
+from typing import Literal
from jinja2 import Template
from pydantic import BaseModel
+from sweagent.exceptions import FormatError, FunctionCallingFormatError
from sweagent.tools.commands import Command
from sweagent.tools.utils import _should_quote
-class FormatError(Exception):
- pass
-
-
-class FunctionCallingFormatError(FormatError):
- """Format error exception used by the function
- calling parser."""
-
- def __init__(
- self,
- message: str,
- error_code: Literal[
- "missing", "multiple", "incorrect_args", "invalid_json", "invalid_command", "missing_arg", "unexpected_arg"
- ],
- **extra_info: Any,
- ):
- super().__init__(message + f" [error_code={error_code}]")
- self.message = message
- self.extra_info = {"error_code": error_code, **extra_info}
-
-
class AbstractParseFunction(ABC):
"""
Abstract class for parsing functions.
diff --git a/tests/test_parsing.py b/tests/test_parsing.py
index e9b6579a..c66d07a1 100644
--- a/tests/test_parsing.py
+++ b/tests/test_parsing.py
@@ -3,12 +3,11 @@ from __future__ import annotations
import pytest
from jinja2 import Template
+from sweagent.exceptions import FormatError, FunctionCallingFormatError
from sweagent.tools.commands import Command
from sweagent.tools.parsing import (
ActionParser,
EditFormat,
- FormatError,
- FunctionCallingFormatError,
FunctionCallingParser,
Identity,
JsonParser,