Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
Fixes missing prompt template or system template (#2408)
Vidit Ostwal59f34d900a06
diff --git a/docs/concepts/agents.mdx b/docs/concepts/agents.mdxindex fa954d4fc..4234e6428 100644--- a/docs/concepts/agents.mdx+++ b/docs/concepts/agents.mdx@@ -255,7 +255,11 @@ custom_agent = Agent(- `response_template`: Formats agent responses<Note>-When using custom templates, you can use variables like `{role}`, `{goal}`, and `{input}` in your templates. These will be automatically populated during execution.+When using custom templates, ensure that both `system_template` and `prompt_template` are defined. The `response_template` is optional but recommended for consistent output formatting.+</Note>++<Note>+When using custom templates, you can use variables like `{role}`, `{goal}`, and `{backstory}` in your templates. These will be automatically populated during execution.</Note>## Agent Toolsdiff --git a/src/crewai/utilities/prompts.py b/src/crewai/utilities/prompts.pyindex 0100f0f07..cd3577874 100644--- a/src/crewai/utilities/prompts.py+++ b/src/crewai/utilities/prompts.py@@ -54,10 +54,12 @@ class Prompts(BaseModel):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 formatprompt_parts = [self.i18n.slice(component) for component in components]prompt = "".join(prompt_parts)else:+ # All templates are provided, use themprompt_parts = [self.i18n.slice(component)for component in components@@ -67,8 +69,12 @@ class Prompts(BaseModel):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.pyindex 94d68cab2..b3d243a53 100644--- a/tests/agent_test.py+++ b/tests/agent_test.py@@ -72,9 +72,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"