Tasks
Detailed guide on managing and creating tasks within the CrewAI framework, reflecting the latest codebase updates.
Overview of a Task
In the CrewAI framework, a Task
is a specific assignment completed by an Agent
.
They 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.
Task Attributes
Attribute | Parameters | Type | Description |
---|---|---|---|
Description | description | str | A clear, concise statement of what the task entails. |
Agent | agent | Optional[BaseAgent] | The agent responsible for the task, assigned either directly or by the crew’s process. |
Expected Output | expected_output | str | A detailed description of what the task’s completion looks like. |
Tools (optional) | tools | Optional[List[Any]] | The functions or capabilities the agent can utilize to perform the task. Defaults to an empty list. |
Async Execution (optional) | async_execution | Optional[bool] | If set, the task executes asynchronously, allowing progression without waiting for completion. Defaults to False. |
Context (optional) | context | Optional[List["Task"]] | Specifies tasks whose outputs are used as context for this task. |
Config (optional) | config | Optional[Dict[str, Any]] | Additional configuration details for the agent executing the task, allowing further customization. Defaults to None. |
Output JSON (optional) | output_json | Optional[Type[BaseModel]] | Outputs a JSON object, requiring an OpenAI client. Only one output format can be set. |
Output Pydantic (optional) | output_pydantic | Optional[Type[BaseModel]] | Outputs a Pydantic model object, requiring an OpenAI client. Only one output format can be set. |
Output File (optional) | output_file | Optional[str] | Saves the task output to a file. If used with Output JSON or Output Pydantic , specifies how the output is saved. |
Output (optional) | output | Optional[TaskOutput] | An instance of TaskOutput , containing the raw, JSON, and Pydantic output plus additional details. |
Callback (optional) | callback | Optional[Any] | A callable that is executed with the task’s output upon completion. |
Human Input (optional) | human_input | Optional[bool] | Indicates if the task should involve human review at the end, useful for tasks needing human oversight. Defaults to False. |
Converter Class (optional) | converter_cls | Optional[Type[Converter]] | A converter class used to export structured output. Defaults to None. |
Creating a Task
Creating a task involves defining its scope, responsible agent, and any additional attributes for flexibility:
Directly specify an agent
for assignment or let the hierarchical
CrewAI’s process decide based on roles, availability, etc.
Task Output
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.
Task Output Attributes
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. |
Task Methods and Properties
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. |
Accessing Task Outputs
Once a task has been executed, its output can be accessed through the output
attribute of the Task
object. The TaskOutput
class provides various ways to interact with and present this output.
Example
Integrating Tools with Tasks
Leverage tools from the CrewAI Toolkit and LangChain Tools for enhanced task performance and agent interaction.
Creating a Task with Tools
This demonstrates how tasks with specific tools can override an agent’s default set for tailored task execution.
Referring to Other Tasks
In CrewAI, the output of one task is automatically relayed into the next one, but you can specifically define what tasks’ output, including multiple, should be used as context for another task.
This is useful when you have a task that depends on the output of another task that is not performed immediately after it. This is done through the context
attribute of the task:
Asynchronous Execution
You can define a task to be executed asynchronously. This means that the crew will not wait for it to be completed to continue with the next task. This is useful for tasks that take a long time to be completed, or that are not crucial for the next tasks to be performed.
You can then use the context
attribute to define in a future task that it should wait for the output of the asynchronous task to be completed.
Callback Mechanism
The callback function is executed after the task is completed, allowing for actions or notifications to be triggered based on the task’s outcome.
Accessing a Specific Task Output
Once a crew finishes running, you can access the output of a specific task by using the output
attribute of the task object:
Tool Override Mechanism
Specifying tools in a task allows for dynamic adaptation of agent capabilities, emphasizing CrewAI’s flexibility.
Error Handling and Validation Mechanisms
While creating and executing tasks, certain validation mechanisms are in place to ensure the robustness and reliability of task attributes. These include but are not limited to:
- Ensuring only one output type is set per task to maintain clear output expectations.
- Preventing the manual assignment of the
id
attribute to uphold the integrity of the unique identifier system.
These validations help in maintaining the consistency and reliability of task executions within the crewAI framework.
Creating Directories when Saving Files
You can now specify if a task should create directories when saving its output to a file. This is particularly useful for organizing outputs and ensuring that file paths are correctly structured.
Conclusion
Tasks are the driving force behind the actions of agents in CrewAI. By properly defining tasks and their outcomes, you set the stage for your AI agents to work effectively, either independently or as a collaborative unit. Equipping tasks with appropriate tools, understanding the execution process, and following robust validation practices are crucial for maximizing CrewAI’s potential, ensuring agents are effectively prepared for their assignments and that tasks are executed as intended.