diff --git a/src/crewai/task.py b/src/crewai/task.py index 754fab491..ddc8ad754 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -135,6 +135,10 @@ class Task(BaseModel): description="Whether the task should have a human review the final answer of the agent", default=False, ) + markdown: Optional[bool] = Field( + description="Whether the task should instruct the agent to return the final answer formatted in Markdown", + default=False, + ) converter_cls: Optional[Type[Converter]] = Field( description="A converter class used to export structured output", default=None, @@ -533,6 +537,11 @@ class Task(BaseModel): expected_output=self.expected_output ) tasks_slices = [self.description, output] + + if self.markdown: + markdown_instruction = "Your final answer MUST be formatted in Markdown syntax." + tasks_slices.append(markdown_instruction) + return "\n".join(tasks_slices) def interpolate_inputs_and_add_conversation_history( diff --git a/tests/test_markdown_task.py b/tests/test_markdown_task.py new file mode 100644 index 000000000..706dc624b --- /dev/null +++ b/tests/test_markdown_task.py @@ -0,0 +1,53 @@ +"""Test the markdown attribute in Task class.""" + +import pytest +from unittest.mock import patch + +from crewai import Agent, Task + + +def test_markdown_option_in_task_prompt(): + """Test that when markdown=True, the task prompt includes markdown formatting instructions.""" + + researcher = Agent( + role="Researcher", + goal="Research a topic", + backstory="You're a researcher specialized in providing well-formatted content.", + allow_delegation=False, + ) + + task = Task( + description="Research advances in AI in 2023", + expected_output="A summary of key AI advances in 2023", + markdown=True, + agent=researcher, + ) + + prompt = task.prompt() + + assert "Research advances in AI in 2023" in prompt + assert "A summary of key AI advances in 2023" in prompt + assert "Your final answer MUST be formatted in Markdown syntax." in prompt + + +def test_markdown_option_not_in_task_prompt_by_default(): + """Test that by default (markdown=False), the task prompt does not include markdown formatting instructions.""" + + researcher = Agent( + role="Researcher", + goal="Research a topic", + backstory="You're a researcher specialized in providing well-formatted content.", + allow_delegation=False, + ) + + task = Task( + description="Research advances in AI in 2023", + expected_output="A summary of key AI advances in 2023", + agent=researcher, + ) + + prompt = task.prompt() + + assert "Research advances in AI in 2023" in prompt + assert "A summary of key AI advances in 2023" in prompt + assert "Your final answer MUST be formatted in Markdown syntax." not in prompt