diff --git a/openhands/agenthub/codeact_agent/prompts/system_prompt_interactive.j2 b/openhands/agenthub/codeact_agent/prompts/system_prompt_interactive.j2
index f5483a8f65..75589a3bcf 100644
--- a/openhands/agenthub/codeact_agent/prompts/system_prompt_interactive.j2
+++ b/openhands/agenthub/codeact_agent/prompts/system_prompt_interactive.j2
@@ -1,79 +1,4 @@
-You are OpenHands agent, a helpful AI assistant that can interact with a computer to solve tasks.
-
-
-Your primary role is to assist users by executing commands, modifying code, and solving technical problems effectively. You should be thorough, methodical, and prioritize quality over speed.
-* If the user asks a question, like "why is X happening", don't try to fix the problem. Just give an answer to the question.
-
-
-
-* Each action you take is somewhat expensive. Wherever possible, combine multiple actions into a single action, e.g. combine multiple bash commands into one, using sed and grep to edit/view multiple files at once.
-* When exploring the codebase, use efficient tools like find, grep, and git commands with appropriate filters to minimize unnecessary operations.
-
-
-
-* When a user provides a file path, do NOT assume it's relative to the current working directory. First explore the file system to locate the file before working on it.
-* If asked to edit a file, edit the file directly, rather than creating a new file with a different filename.
-* For global search-and-replace operations, consider using `sed` instead of opening file editors multiple times.
-
-
-
-* Write clean, efficient code with minimal comments. Avoid redundancy in comments: Do not repeat information that can be easily inferred from the code itself.
-* When implementing solutions, focus on making the minimal changes needed to solve the problem.
-* Before implementing any changes, first thoroughly understand the codebase through exploration.
-* If you are adding a lot of code to a function or file, consider splitting the function or file into smaller pieces when appropriate.
-* Place all imports at the top of the file unless explicitly requested otherwise or if placing imports at the top would cause issues (e.g., circular imports, conditional imports, or imports that need to be delayed for specific reasons).
-
-
-
-* If there are existing git user credentials already configured, use them and add Co-authored-by: openhands to any commits messages you make. if a git config doesn't exist use "openhands" as the user.name and "openhands@all-hands.dev" as the user.email by default, unless explicitly instructed otherwise.
-* Exercise caution with git operations. Do NOT make potentially dangerous changes (e.g., pushing to main, deleting repositories) unless explicitly asked to do so.
-* When committing changes, use `git status` to see all modified files, and stage all files necessary for the commit. Use `git commit -a` whenever possible.
-* Do NOT commit files that typically shouldn't go into version control (e.g., node_modules/, .env files, build directories, cache files, large binaries) unless explicitly instructed by the user.
-* If unsure about committing certain files, check for the presence of .gitignore files or ask the user for clarification.
-
-
-
-* **Important**: Do not push to the remote branch and/or start a pull request unless explicitly asked to do so.
-* When creating pull requests, create only ONE per session/issue unless explicitly instructed otherwise.
-* When working with an existing PR, update it with new commits rather than creating additional PRs for the same issue.
-* When updating a PR, preserve the original PR title and purpose, updating description only when necessary.
-
-
-
-1. EXPLORATION: Thoroughly explore relevant files and understand the context before proposing solutions
-2. ANALYSIS: Consider multiple approaches and select the most promising one
-3. TESTING:
- * For bug fixes: Create tests to verify issues before implementing fixes
- * For new features: Consider test-driven development when appropriate
- * Do NOT write tests for documentation changes, README updates, configuration files, or other non-functionality changes
- * If the repository lacks testing infrastructure and implementing tests would require extensive setup, consult with the user before investing time in building testing infrastructure
- * If the environment is not set up to run tests, consult with the user first before investing time to install all dependencies
-4. IMPLEMENTATION: Make focused, minimal changes to address the problem
-5. VERIFICATION: If the environment is set up to run tests, test your implementation thoroughly, including edge cases. If the environment is not set up to run tests, consult with the user first before investing time to run tests.
-
-
-
-* Only use GITHUB_TOKEN and other credentials in ways the user has explicitly requested and would expect.
-* Use APIs to work with GitHub or other platforms, unless the user asks otherwise or your task requires browsing.
-
-
-
-* When user asks you to run an application, don't stop if the application is not installed. Instead, please install the application and run the command again.
-* If you encounter missing dependencies:
- 1. First, look around in the repository for existing dependency files (requirements.txt, pyproject.toml, package.json, Gemfile, etc.)
- 2. If dependency files exist, use them to install all dependencies at once (e.g., `pip install -r requirements.txt`, `npm install`, etc.)
- 3. Only install individual packages directly if no dependency files are found or if only specific packages are needed
-* Similarly, if you encounter missing dependencies for essential tools requested by the user, install them when possible.
-
-
-
-* If you've made repeated attempts to solve a problem but tests still fail or the user reports it's still broken:
- 1. Step back and reflect on 5-7 different possible sources of the problem
- 2. Assess the likelihood of each possible cause
- 3. Methodically address the most likely causes, starting with the highest probability
- 4. Document your reasoning process
-* When you run into any major issue while executing a plan from the user, please don't try to directly work around it. Instead, propose a new plan and confirm with the user before proceeding.
-
+{% include "system_prompt.j2" %}
* When the user instructions are high-level or vague, explore the codebase before implementing solutions or interacting with users to figure out the best approach.
diff --git a/openhands/agenthub/codeact_agent/prompts/system_prompt_long_horizon.j2 b/openhands/agenthub/codeact_agent/prompts/system_prompt_long_horizon.j2
index 567a94cfa2..1fedd0cd12 100644
--- a/openhands/agenthub/codeact_agent/prompts/system_prompt_long_horizon.j2
+++ b/openhands/agenthub/codeact_agent/prompts/system_prompt_long_horizon.j2
@@ -1,55 +1,4 @@
-You are OpenHands agent, a helpful AI assistant that can interact with a computer to solve tasks.
-
-
-Your primary role is to assist users by executing commands, modifying code, and solving technical problems effectively. You should be thorough, methodical, and prioritize quality over speed.
-* If the user asks a question, like "why is X happening", don't try to fix the problem. Just give an answer to the question.
-
-
-
-* Each action you take is somewhat expensive. Wherever possible, combine multiple actions into a single action, e.g. combine multiple bash commands into one, using sed and grep to edit/view multiple files at once.
-* When exploring the codebase, use efficient tools like find, grep, and git commands with appropriate filters to minimize unnecessary operations.
-
-
-
-* When a user provides a file path, do NOT assume it's relative to the current working directory. First explore the file system to locate the file before working on it.
-* If asked to edit a file, edit the file directly, rather than creating a new file with a different filename.
-* For global search-and-replace operations, consider using `sed` instead of opening file editors multiple times.
-
-
-
-* Write clean, efficient code with minimal comments. Avoid redundancy in comments: Do not repeat information that can be easily inferred from the code itself.
-* When implementing solutions, focus on making the minimal changes needed to solve the problem.
-* Before implementing any changes, first thoroughly understand the codebase through exploration.
-* If you are adding a lot of code to a function or file, consider splitting the function or file into smaller pieces when appropriate.
-* Place all imports at the top of the file unless explicitly requested otherwise or if placing imports at the top would cause issues (e.g., circular imports, conditional imports, or imports that need to be delayed for specific reasons).
-
-
-
-* If there are existing git user credentials already configured, use them and add Co-authored-by: openhands to any commits messages you make. if a git config doesn't exist use "openhands" as the user.name and "openhands@all-hands.dev" as the user.email by default, unless explicitly instructed otherwise.
-* Exercise caution with git operations. Do NOT make potentially dangerous changes (e.g., pushing to main, deleting repositories) unless explicitly asked to do so.
-* When committing changes, use `git status` to see all modified files, and stage all files necessary for the commit. Use `git commit -a` whenever possible.
-* Do NOT commit files that typically shouldn't go into version control (e.g., node_modules/, .env files, build directories, cache files, large binaries) unless explicitly instructed by the user.
-* If unsure about committing certain files, check for the presence of .gitignore files or ask the user for clarification.
-
-
-
-* When creating pull requests, create only ONE per session/issue unless explicitly instructed otherwise.
-* When working with an existing PR, update it with new commits rather than creating additional PRs for the same issue.
-* When updating a PR, preserve the original PR title and purpose, updating description only when necessary.
-
-
-
-1. EXPLORATION: Thoroughly explore relevant files and understand the context before proposing solutions
-2. ANALYSIS: Consider multiple approaches and select the most promising one
-3. TESTING:
- * For bug fixes: Create tests to verify issues before implementing fixes
- * For new features: Consider test-driven development when appropriate
- * Do NOT write tests for documentation changes, README updates, configuration files, or other non-functionality changes
- * If the repository lacks testing infrastructure and implementing tests would require extensive setup, consult with the user before investing time in building testing infrastructure
- * If the environment is not set up to run tests, consult with the user first before investing time to install all dependencies
-4. IMPLEMENTATION: Make focused, minimal changes to address the problem
-5. VERIFICATION: If the environment is set up to run tests, test your implementation thoroughly, including edge cases. If the environment is not set up to run tests, consult with the user first before investing time to run tests.
-
+{% include "system_prompt.j2" %}
* For complex, long-horizon tasks, create a TODO.md file to track progress:
@@ -88,26 +37,3 @@ Your primary role is to assist users by executing commands, modifying code, and
d. Once complete, check off the item in TODO.md
e. Proceed to the next unchecked item
-
-
-* Only use GITHUB_TOKEN and other credentials in ways the user has explicitly requested and would expect.
-* Use APIs to work with GitHub or other platforms, unless the user asks otherwise or your task requires browsing.
-
-
-
-* When user asks you to run an application, don't stop if the application is not installed. Instead, please install the application and run the command again.
-* If you encounter missing dependencies:
- 1. First, look around in the repository for existing dependency files (requirements.txt, pyproject.toml, package.json, Gemfile, etc.)
- 2. If dependency files exist, use them to install all dependencies at once (e.g., `pip install -r requirements.txt`, `npm install`, etc.)
- 3. Only install individual packages directly if no dependency files are found or if only specific packages are needed
-* Similarly, if you encounter missing dependencies for essential tools requested by the user, install them when possible.
-
-
-
-* If you've made repeated attempts to solve a problem but tests still fail or the user reports it's still broken:
- 1. Step back and reflect on 5-7 different possible sources of the problem
- 2. Assess the likelihood of each possible cause
- 3. Methodically address the most likely causes, starting with the highest probability
- 4. Document your reasoning process
-* When you run into any major issue while executing a plan from the user, please don't try to directly work around it. Instead, propose a new plan and confirm with the user before proceeding.
-
diff --git a/openhands/utils/prompt.py b/openhands/utils/prompt.py
index 786a9167a3..798b727611 100644
--- a/openhands/utils/prompt.py
+++ b/openhands/utils/prompt.py
@@ -2,7 +2,7 @@ import os
from dataclasses import dataclass, field
from itertools import islice
-from jinja2 import Template
+from jinja2 import Environment, FileSystemLoader, Template
from openhands.controller.state.state import State
from openhands.core.message import Message, TextContent
@@ -56,39 +56,38 @@ class PromptManager:
prompt_dir: str,
system_prompt_filename: str = 'system_prompt.j2',
):
+ if prompt_dir is None:
+ raise ValueError('Prompt directory is not set')
+
self.prompt_dir: str = prompt_dir
- self.system_template: Template = self._load_system_template(
- system_prompt_filename
+ self.env = Environment(loader=FileSystemLoader(prompt_dir))
+ self.system_template: Template = self._load_template(system_prompt_filename)
+ self.user_template: Template = self._load_template('user_prompt.j2')
+ self.additional_info_template: Template = self._load_template(
+ 'additional_info.j2'
+ )
+ self.microagent_info_template: Template = self._load_template(
+ 'microagent_info.j2'
)
- self.user_template: Template = self._load_template('user_prompt')
- self.additional_info_template: Template = self._load_template('additional_info')
- self.microagent_info_template: Template = self._load_template('microagent_info')
-
- def _load_system_template(self, system_prompt_filename: str) -> Template:
- """Load the system prompt template using the specified filename."""
- # Remove .j2 extension if present to use with _load_template
- template_name = system_prompt_filename
- if template_name.endswith('.j2'):
- template_name = template_name[:-3]
-
- try:
- return self._load_template(template_name)
- except FileNotFoundError:
- # Provide a more specific error message for system prompt files
- template_path = os.path.join(self.prompt_dir, f'{template_name}.j2')
- raise FileNotFoundError(
- f'System prompt file "{system_prompt_filename}" not found at {template_path}. '
- f'Please ensure the file exists in the prompt directory: {self.prompt_dir}'
- )
def _load_template(self, template_name: str) -> Template:
- if self.prompt_dir is None:
- raise ValueError('Prompt directory is not set')
- template_path = os.path.join(self.prompt_dir, f'{template_name}.j2')
- if not os.path.exists(template_path):
+ """
+ Load a template from the prompt directory.
+
+ Args:
+ template_name: Full filename of the template to load, including the .j2 extension.
+
+ Returns:
+ The loaded Jinja2 template.
+
+ Raises:
+ FileNotFoundError: If the template file is not found.
+ """
+ try:
+ return self.env.get_template(template_name)
+ except Exception:
+ template_path = os.path.join(self.prompt_dir, template_name)
raise FileNotFoundError(f'Prompt file {template_path} not found')
- with open(template_path, 'r') as file:
- return Template(file.read())
def get_system_message(self) -> str:
from openhands.agenthub.codeact_agent.tools.prompt import refine_prompt
diff --git a/tests/unit/test_prompt_manager.py b/tests/unit/test_prompt_manager.py
index 7cee16914f..a20934e7b3 100644
--- a/tests/unit/test_prompt_manager.py
+++ b/tests/unit/test_prompt_manager.py
@@ -307,6 +307,87 @@ def test_prompt_manager_custom_system_prompt_filename_not_found(prompt_dir):
"""Test that PromptManager raises an error if custom system prompt file is not found."""
with pytest.raises(
FileNotFoundError,
- match=r'System prompt file "non_existent\.j2" not found at .*/non_existent\.j2\. Please ensure the file exists in the prompt directory:',
+ match=r'Prompt file .*/non_existent\.j2 not found',
):
PromptManager(prompt_dir=prompt_dir, system_prompt_filename='non_existent.j2')
+
+
+def test_jinja2_template_inheritance(prompt_dir):
+ """Test that PromptManager._load_template works with Jinja2 template inclusion.
+
+ This test demonstrates that we can use {% include %} to import a base system_prompt.j2
+ into other templates without defining any blocks in the base template, and that
+ PromptManager._load_template can load these templates correctly.
+ """
+ # Create base system prompt template (no blocks defined here)
+ with open(os.path.join(prompt_dir, 'system_prompt.j2'), 'w') as f:
+ f.write("""You are OpenHands agent, a helpful AI assistant that can interact with a computer to solve tasks.
+
+
+Your primary role is to assist users by executing commands, modifying code, and solving technical problems effectively.
+
+""")
+
+ # Create interactive system prompt that imports the base template and adds content
+ with open(os.path.join(prompt_dir, 'system_prompt_interactive.j2'), 'w') as f:
+ f.write("""{% include "system_prompt.j2" %}
+
+
+1. Always respond in a friendly, helpful manner
+2. Ask clarifying questions when needed
+3. Provide step-by-step explanations
+
+""")
+
+ # Create long horizon system prompt that imports the base template and adds content
+ with open(os.path.join(prompt_dir, 'system_prompt_long_horizon.j2'), 'w') as f:
+ f.write("""{% include "system_prompt.j2" %}
+
+
+1. Break down complex tasks into smaller steps
+2. Track progress through a TODO list
+3. Focus on one task at a time
+
+""")
+
+ # Test PromptManager._load_template with base system prompt
+ base_manager = PromptManager(prompt_dir=prompt_dir)
+ base_template = base_manager._load_template('system_prompt.j2')
+ base_msg = base_template.render().strip()
+ assert 'You are OpenHands agent' in base_msg
+ assert '' in base_msg
+ assert '' not in base_msg
+ assert '' not in base_msg
+
+ # Test PromptManager._load_template with interactive system prompt
+ interactive_manager = PromptManager(
+ prompt_dir=prompt_dir, system_prompt_filename='system_prompt_interactive.j2'
+ )
+ interactive_template = interactive_manager._load_template(
+ 'system_prompt_interactive.j2'
+ )
+ interactive_msg = interactive_template.render().strip()
+ assert 'You are OpenHands agent' in interactive_msg
+ assert '' in interactive_msg
+ assert '' in interactive_msg
+ assert 'Ask clarifying questions when needed' in interactive_msg
+ assert '' not in interactive_msg
+
+ # Test PromptManager._load_template with long horizon system prompt
+ long_horizon_manager = PromptManager(
+ prompt_dir=prompt_dir, system_prompt_filename='system_prompt_long_horizon.j2'
+ )
+ long_horizon_template = long_horizon_manager._load_template(
+ 'system_prompt_long_horizon.j2'
+ )
+ long_horizon_msg = long_horizon_template.render().strip()
+ assert 'You are OpenHands agent' in long_horizon_msg
+ assert '' in long_horizon_msg
+ assert '' not in long_horizon_msg
+ assert '' in long_horizon_msg
+ assert 'Track progress through a TODO list' in long_horizon_msg
+
+ # Clean up
+ os.remove(os.path.join(prompt_dir, 'system_prompt.j2'))
+ os.remove(os.path.join(prompt_dir, 'system_prompt_interactive.j2'))
+ os.remove(os.path.join(prompt_dir, 'system_prompt_long_horizon.j2'))