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/change: tool argument_format is jinja format +fix --view_range

Kilian Lieret1a2e74ca60d9
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 1965fe2d..7770f364 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -37,22 +37,11 @@ from sweagent.tools.parsing import (
from sweagent.tools.tools import ToolConfig, ToolHandler
from sweagent.types import AgentInfo, AgentRunResult, History, StepOutput, Trajectory, TrajectoryStep
from sweagent.utils.config import _convert_paths_to_abspath, _strip_abspath_from_dict
+from sweagent.utils.jinja_warnings import _warn_probably_wrong_jinja_syntax
from sweagent.utils.log import get_logger
from sweagent.utils.patch_formatter import PatchFormatter
-def _warn_probably_wrong_jinja_syntax(template: str | None) -> None:
- if template is None:
- return
- if "{" not in template:
- return
- for s in ["{%", "{ %", "{{"]:
- if s in template:
- return
- logger = get_logger("swea-config", emoji="⚙️")
- logger.warning("Probably wrong Jinja syntax in template: %s. Make sure to use {{var}} instead of {var}.", template)
-
-
class TemplateConfig(BaseModel):
"""This configuration is used to define almost all message templates that are
formatted by the agent and sent to the LM.
diff --git a/sweagent/tools/commands.py b/sweagent/tools/commands.py
index 0b2e3b7d..ced75cbf 100644
--- a/sweagent/tools/commands.py
+++ b/sweagent/tools/commands.py
@@ -22,7 +22,9 @@ import re
import string
from functools import cached_property
-from pydantic import BaseModel, model_validator
+from pydantic import BaseModel, field_validator, model_validator
+
+from sweagent.utils.jinja_warnings import _warn_probably_wrong_jinja_syntax
ARGUMENT_NAME_PATTERN = r"[a-zA-Z_][a-zA-Z0-9_-]+"
@@ -64,8 +66,13 @@ class Argument(BaseModel):
description: str
required: bool
enum: list[str] | None = None
- argument_format: str = "{value}"
- """How to invoke the argument in the command"""
+ argument_format: str = "{{value}}"
+ """How to invoke the argument in the command. Make sure to use jinja syntax ({{value}}) instead of {value})."""
+
+ @field_validator("argument_format")
+ def validate_argument_format(cls, value: str) -> str:
+ _warn_probably_wrong_jinja_syntax(value)
+ return value
class Command(BaseModel):
diff --git a/sweagent/tools/parsing.py b/sweagent/tools/parsing.py
index 1ebc9092..c08df4d6 100644
--- a/sweagent/tools/parsing.py
+++ b/sweagent/tools/parsing.py
@@ -8,6 +8,7 @@ from shlex import quote
from textwrap import dedent
from typing import Any, Literal
+from jinja2 import Template
from pydantic import BaseModel
from sweagent.tools.commands import Command
@@ -291,7 +292,7 @@ class FunctionCallingParser(AbstractParseFunction, BaseModel):
msg = f"Unexpected argument(s): {', '.join(extra_args)}"
raise FunctionCallingFormatError(msg, "unexpected_arg")
formatted_args = {
- arg.name: arg.argument_format.format(
+ arg.name: Template(arg.argument_format).render(
value=quote(values[arg.name]) if _should_quote(values[arg.name], command) else values[arg.name]
)
if arg.name in values
@@ -389,7 +390,7 @@ class JsonParser(AbstractParseFunction, BaseModel):
value = data_command["arguments"][arg.name]
if _should_quote(value, command):
value = quote(value)
- formatted_args[arg.name] = arg.argument_format.format(value=value)
+ formatted_args[arg.name] = Template(arg.argument_format).render(value=value)
elif strict and arg.required:
msg = f"Required argument '{arg.name}' missing for command '{command.name}'"
raise FormatError(msg)
diff --git a/sweagent/utils/jinja_warnings.py b/sweagent/utils/jinja_warnings.py
new file mode 100644
index 00000000..5a4b56c9
--- /dev/null
+++ b/sweagent/utils/jinja_warnings.py
@@ -0,0 +1,14 @@
+from sweagent.utils.log import get_logger
+
+
+def _warn_probably_wrong_jinja_syntax(template: str | None) -> None:
+ """Warn if the template uses {var} instead of {{var}}."""
+ if template is None:
+ return
+ if "{" not in template:
+ return
+ for s in ["{%", "{ %", "{{"]:
+ if s in template:
+ return
+ logger = get_logger("swea-config", emoji="⚙️")
+ logger.warning("Probably wrong Jinja syntax in template: %s. Make sure to use {{var}} instead of {var}.", template)
diff --git a/tools/edit_anthropic/config.yaml b/tools/edit_anthropic/config.yaml
index 19058ed5..c3759556 100644
--- a/tools/edit_anthropic/config.yaml
+++ b/tools/edit_anthropic/config.yaml
@@ -29,7 +29,7 @@ tools:
type: string
description: "Text to insert when evoking `create` (required for `create`)"
required: false
- argument_format: "--file_text {value}"
+ argument_format: "--file_text {{value}}"
- name: view_range
type: array
items:
@@ -41,20 +41,20 @@ tools:
Indexing at 1 to start.
Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.
required: false
- argument_format: "--view_range {value}"
+ argument_format: "--view_range {{value|join(' ')}}"
- name: old_str
type: string
description: "String to replace when evoking `str_replace`"
required: false
- argument_format: "--old_str {value}"
+ argument_format: "--old_str {{value}}"
- name: new_str
type: string
description: "Replacement string when evoking `str_replace`"
required: false
- argument_format: "--new_str {value}"
+ argument_format: "--new_str {{value}}"
- name: insert_line
type: integer
description: "Required for `insert`: Line number to insert text after."
required: false
- argument_format: "--insert_line {value}"
-state_command: "_state_anthropic"
\ No newline at end of file
+ argument_format: "--insert_line {{value}}"
+state_command: "_state_anthropic"