diff --git a/src/crewai/utilities/prompts.py b/src/crewai/utilities/prompts.py index 66909644f0..84cd67dfac 100644 --- a/src/crewai/utilities/prompts.py +++ b/src/crewai/utilities/prompts.py @@ -54,10 +54,12 @@ def _build_prompt( response_template=None, ) -> str: """Constructs a prompt string from specified components.""" - if not system_template and not prompt_template: + if not system_template or not prompt_template: + # If any of the required templates are missing, fall back to the default format prompt_parts = [self.i18n.slice(component) for component in components] prompt = "".join(prompt_parts) else: + # All templates are provided, use them prompt_parts = [ self.i18n.slice(component) for component in components @@ -67,8 +69,12 @@ def _build_prompt( prompt = prompt_template.replace( "{{ .Prompt }}", "".join(self.i18n.slice("task")) ) - response = response_template.split("{{ .Response }}")[0] - prompt = f"{system}\n{prompt}\n{response}" + # Handle missing response_template + if response_template: + response = response_template.split("{{ .Response }}")[0] + prompt = f"{system}\n{prompt}\n{response}" + else: + prompt = f"{system}\n{prompt}" prompt = ( prompt.replace("{goal}", self.agent.goal) diff --git a/tests/agent_test.py b/tests/agent_test.py index b5b3aae931..857eebf2a5 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -71,9 +71,54 @@ def test_agent_creation(): assert agent.role == "test role" assert agent.goal == "test goal" assert agent.backstory == "test backstory" - assert agent.tools == [] +def test_agent_with_only_system_template(): + """Test that an agent with only system_template works without errors.""" + agent = Agent( + role="Test Role", + goal="Test Goal", + backstory="Test Backstory", + allow_delegation=False, + system_template="You are a test agent...", + # prompt_template is intentionally missing + ) + + assert agent.role == "Test Role" + assert agent.goal == "Test Goal" + assert agent.backstory == "Test Backstory" + +def test_agent_with_only_prompt_template(): + """Test that an agent with only system_template works without errors.""" + agent = Agent( + role="Test Role", + goal="Test Goal", + backstory="Test Backstory", + allow_delegation=False, + prompt_template="You are a test agent...", + # prompt_template is intentionally missing + ) + + assert agent.role == "Test Role" + assert agent.goal == "Test Goal" + assert agent.backstory == "Test Backstory" + + +def test_agent_with_missing_response_template(): + """Test that an agent with system_template and prompt_template but no response_template works without errors.""" + agent = Agent( + role="Test Role", + goal="Test Goal", + backstory="Test Backstory", + allow_delegation=False, + system_template="You are a test agent...", + prompt_template="This is a test prompt...", + # response_template is intentionally missing + ) + assert agent.role == "Test Role" + assert agent.goal == "Test Goal" + assert agent.backstory == "Test Backstory" + def test_agent_default_values(): agent = Agent(role="test role", goal="test goal", backstory="test backstory") assert agent.llm.model == "gpt-4o-mini"