mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-07 23:28:30 +00:00
Fix issue #2402: Handle missing templates gracefully
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -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 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 @@ 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)
|
||||
|
||||
42
test_fix_2402.py
Normal file
42
test_fix_2402.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# test_fix_2402.py
|
||||
from crewai import Agent, Task, Crew
|
||||
|
||||
# Case 1: Only system_template provided
|
||||
agent1 = Agent(
|
||||
role="Test Role",
|
||||
goal="Test Goal",
|
||||
backstory="Test Backstory",
|
||||
system_template="You are a test agent...",
|
||||
# prompt_template is intentionally missing
|
||||
)
|
||||
|
||||
# Case 2: system_template and prompt_template provided, but response_template missing
|
||||
agent2 = Agent(
|
||||
role="Test Role",
|
||||
goal="Test Goal",
|
||||
backstory="Test Backstory",
|
||||
system_template="You are a test agent...",
|
||||
prompt_template="This is a test prompt...",
|
||||
# response_template is intentionally missing
|
||||
)
|
||||
|
||||
# Create tasks and crews
|
||||
task1 = Task(description="Test task 1", agent=agent1, expected_output="Test output 1")
|
||||
task2 = Task(description="Test task 2", agent=agent2, expected_output="Test output 2")
|
||||
|
||||
crew1 = Crew(agents=[agent1], tasks=[task1])
|
||||
crew2 = Crew(agents=[agent2], tasks=[task2])
|
||||
|
||||
print("Testing agent with only system_template...")
|
||||
try:
|
||||
agent1.execute_task(task1)
|
||||
print("Success! No error was raised.")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
print("\nTesting agent with missing response_template...")
|
||||
try:
|
||||
agent2.execute_task(task2)
|
||||
print("Success! No error was raised.")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
47
tests/test_templates.py
Normal file
47
tests/test_templates.py
Normal 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")
|
||||
Reference in New Issue
Block a user