Detailed guide on managing and creating tasks within the CrewAI framework.
Task
is a specific assignment completed by an Agent
.
Tasks provide all necessary details for execution, such as a description, the agent responsible, required tools, and more, facilitating a wide range of action complexities.
Tasks within CrewAI can be collaborative, requiring multiple agents to work together. This is managed through the task properties and orchestrated by the Crew’s process, enhancing teamwork and efficiency.
Attribute | Parameters | Type | Description |
---|---|---|---|
Description | description | str | A clear, concise statement of what the task entails. |
Expected Output | expected_output | str | A detailed description of what the task’s completion looks like. |
Name (optional) | name | Optional[str] | A name identifier for the task. |
Agent (optional) | agent | Optional[BaseAgent] | The agent responsible for executing the task. |
Tools (optional) | tools | List[BaseTool] | The tools/resources the agent is limited to use for this task. |
Context (optional) | context | Optional[List["Task"]] | Other tasks whose outputs will be used as context for this task. |
Async Execution (optional) | async_execution | Optional[bool] | Whether the task should be executed asynchronously. Defaults to False. |
Human Input (optional) | human_input | Optional[bool] | Whether the task should have a human review the final answer of the agent. Defaults to False. |
Markdown (optional) | markdown | Optional[bool] | Whether the task should instruct the agent to return the final answer formatted in Markdown. Defaults to False. |
Config (optional) | config | Optional[Dict[str, Any]] | Task-specific configuration parameters. |
Output File (optional) | output_file | Optional[str] | File path for storing the task output. |
Create Directory (optional) | create_directory | Optional[bool] | Whether to create the directory for output_file if it doesn’t exist. Defaults to True. |
Output JSON (optional) | output_json | Optional[Type[BaseModel]] | A Pydantic model to structure the JSON output. |
Output Pydantic (optional) | output_pydantic | Optional[Type[BaseModel]] | A Pydantic model for task output. |
Callback (optional) | callback | Optional[Any] | Function/object to be executed after task completion. |
Guardrail (optional) | guardrail | Optional[Callable] | Function to validate task output before proceeding to next task. |
src/latest_ai_development/config/tasks.yaml
file and modify the template to match your specific task requirements.
{topic}
) will be replaced with values from your inputs when running the crew:CrewBase
:
agents.yaml
and tasks.yaml
) should match the method names in your Python code.agent
for assignment or let the hierarchical
CrewAI’s process decide based on roles, availability, etc.TaskOutput
class, which supports multiple output formats and can be easily passed between tasks.
The output of a task in CrewAI framework is encapsulated within the TaskOutput
class. This class provides a structured way to access results of a task, including various formats such as raw output, JSON, and Pydantic models.
By default, the TaskOutput
will only include the raw
output. A TaskOutput
will only include the pydantic
or json_dict
output if the original Task
object was configured with output_pydantic
or output_json
, respectively.
Attribute | Parameters | Type | Description |
---|---|---|---|
Description | description | str | Description of the task. |
Summary | summary | Optional[str] | Summary of the task, auto-generated from the first 10 words of the description. |
Raw | raw | str | The raw output of the task. This is the default format for the output. |
Pydantic | pydantic | Optional[BaseModel] | A Pydantic model object representing the structured output of the task. |
JSON Dict | json_dict | Optional[Dict[str, Any]] | A dictionary representing the JSON output of the task. |
Agent | agent | str | The agent that executed the task. |
Output Format | output_format | OutputFormat | The format of the task output, with options including RAW, JSON, and Pydantic. The default is RAW. |
Method/Property | Description |
---|---|
json | Returns the JSON string representation of the task output if the output format is JSON. |
to_dict | Converts the JSON and Pydantic outputs to a dictionary. |
str | Returns the string representation of the task output, prioritizing Pydantic, then JSON, then raw. |
output
attribute of the Task
object. The TaskOutput
class provides various ways to interact with and present this output.
markdown
parameter enables automatic markdown formatting for task outputs. When set to True
, the task will instruct the agent to format the final answer using proper Markdown syntax.
markdown=True
, the agent will receive additional instructions to format the output using:
#
for headers**text**
for bold text*text*
for italic text-
or *
for bullet points`code`
for inline code
language ``` for code blocksmarkdown=True
, so you don’t need to specify formatting requirements in your task description.context
attribute. For example:
guardrail
parameter:
(bool, Any)
(bool, Any)
. For example: (True, validated_result)
(bool, str)
. For example: (False, "Error message explain the failure")
(False, error)
:
(True, result)
output_pydantic
output_pydantic
property allows you to define a Pydantic model that the task output should conform to. This ensures that the output is not only structured but also validated according to the Pydantic model.
Here’s an example demonstrating how to use output_pydantic:
output_json
output_json
property allows you to define the expected output in JSON format. This ensures that the task’s output is a valid JSON structure that can be easily parsed and used in your application.
Here’s an example demonstrating how to use output_json
:
context
attribute of the task:
context
attribute to define in a future task that it should wait for the output of the asynchronous task to be completed.
output
attribute of the task object:
id
attribute to uphold the integrity of the unique identifier system.create_directory
parameter controls whether CrewAI should automatically create directories when saving task outputs to files. This feature is particularly useful for organizing outputs and ensuring that file paths are correctly structured, especially when working with complex project hierarchies.
create_directory=True
, which means CrewAI will automatically create any missing directories in the output file path:
create_directory=False
:
create_directory=True
):
create_directory=False
):
create_directory=False
and the directory doesn’t exist, CrewAI will raise a RuntimeError
: