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]

Fix: Proper forwarding of step vars to templates

Kilian Lieret9bc6c3dda7c4
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index a1924c24..e6e85348 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -500,7 +500,9 @@ class Agent:
assert self.traj_path is not None
self.traj_path.write_text(json.dumps(data, indent=2))
- def get_model_requery_history(self, error_template: str, *, output: str, **kwargs: str) -> list[dict[str, str]]:
+ def get_model_requery_history(
+ self, error_template: str, *, output: str, **kwargs: str | int | float | bool | None
+ ) -> list[dict[str, str]]:
"""Ask the model to correct after a hitting one of the following errors:
1. Malformatted output (could not parse action)
@@ -718,7 +720,7 @@ class Agent:
def handle_error_with_retry(exception: Exception, template: str) -> list[dict[str, str]]:
"""Requeries the model if the error is a format/blocklist/bash syntax error."""
- step = getattr(exception, "step", StepOutput())
+ step: StepOutput = getattr(exception, "step", StepOutput())
self.add_step_to_trajectory(step)
exception_message = getattr(exception, "message", "")
if not exception_message:
@@ -728,7 +730,7 @@ class Agent:
pass
return self.get_model_requery_history(
error_template=template,
- **step.model_dump(),
+ **step.to_template_format_dict(),
**getattr(exception, "extra_info", {}),
exception_message=exception_message,
)
diff --git a/sweagent/types.py b/sweagent/types.py
index d213d042..c9fea4bd 100644
--- a/sweagent/types.py
+++ b/sweagent/types.py
@@ -29,6 +29,16 @@ class StepOutput(BaseModel):
tool_call_ids: list[str] | None = None
"""State of the environment at the end of the step"""
+ def to_template_format_dict(self) -> dict[str, str | int | float | bool | None]:
+ """Used for formatting (error) prompt templates"""
+ out = {}
+ for k, v in self.model_dump().items():
+ if k in ("tool_calls", "tool_call_ids", "state"):
+ continue
+ out[k] = v
+ out |= self.state
+ return out
+
class TrajectoryStep(TypedDict):
action: str