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]

Warn if templates don't use jinja syntax

Kilian Lieret01ee05a4095d
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 09694acc..fb0da727 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -8,7 +8,7 @@ from pathlib import Path
from typing import Any
from jinja2 import Template
-from pydantic import BaseModel, ConfigDict, Field
+from pydantic import BaseModel, ConfigDict, Field, model_validator
from simple_parsing.helpers.fields import field
from swerex.exceptions import BashIncorrectSyntaxError, CommandTimeoutError, SweRexception
from tenacity import RetryError
@@ -40,6 +40,18 @@ 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.
@@ -86,6 +98,18 @@ class TemplateConfig(BaseModel):
if self.next_step_no_output_template is None:
self.next_step_no_output_template = self.next_step_template
+ @model_validator(mode="after")
+ @classmethod
+ def validate_template_jinja_syntax(cls, data: Any) -> Any:
+ template_fields = [field for field in cls.model_fields.keys() if field.endswith("_template")]
+ for field in template_fields:
+ if isinstance(data, dict):
+ value = data[field]
+ else:
+ value = getattr(data, field)
+ _warn_probably_wrong_jinja_syntax(value)
+ return data
+
class AgentConfig(BaseModel):
"""This configuration object specifies the behavior of an agent."""