Add markdown attribute to Task class for formatting responses in Markdown

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-20 05:39:49 +00:00
parent bef5971598
commit 2c26ab27c0
2 changed files with 62 additions and 0 deletions

View File

@@ -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(

View File

@@ -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