Fix issue #2402: Handle missing templates gracefully

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-19 06:33:45 +00:00
parent fe0813e831
commit 4a5674bc6c
3 changed files with 98 additions and 3 deletions

47
tests/test_templates.py Normal file
View File

@@ -0,0 +1,47 @@
"""Test template handling in Agent creation."""
import pytest
from crewai import Agent, Task, Crew
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
)
task = Task(description="Test task", agent=agent, expected_output="Test output")
# This should not raise an error
try:
agent.execute_task(task)
assert True
except AttributeError:
pytest.fail("AttributeError was raised with only system_template")
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
)
task = Task(description="Test task", agent=agent, expected_output="Test output")
# This should not raise an error
try:
agent.execute_task(task)
assert True
except AttributeError:
pytest.fail("AttributeError was raised with missing response_template")