Tasks
Detailed guide on managing and creating tasks within the CrewAI framework.
Overview of a Task
In the CrewAI framework, a 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.
Task Execution Flow
Tasks can be executed in two ways:
- Sequential: Tasks are executed in the order they are defined
- Hierarchical: Tasks are assigned to agents based on their roles and expertise
The execution flow is defined when creating the crew:
Task Attributes
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. |
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. |
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. |
Creating Tasks
There are two ways to create tasks in CrewAI: using YAML configuration (recommended) or defining them directly in code.
YAML Configuration (Recommended)
Using YAML configuration provides a cleaner, more maintainable way to define tasks. We strongly recommend using this approach to define tasks in your CrewAI projects.
After creating your CrewAI project as outlined in the Installation section, navigate to the src/latest_ai_development/config/tasks.yaml
file and modify the template to match your specific task requirements.
Variables in your YAML files (like {topic}
) will be replaced with values from your inputs when running the crew:
Here’s an example of how to configure tasks using YAML:
To use this YAML configuration in your code, create a crew class that inherits from CrewBase
:
The names you use in your YAML files (agents.yaml
and tasks.yaml
) should match the method names in your Python code.
Direct Code Definition (Alternative)
Alternatively, you can define tasks directly in your code without using YAML configuration:
Directly specify an agent
for assignment or let the hierarchical
CrewAI’s process decide based on roles, availability, etc.
Task Output
Understanding task outputs is crucial for building effective AI workflows. CrewAI provides a structured way to handle task results through the 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.
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
Task Dependencies and Context
Tasks can depend on the output of other tasks using the context
attribute. For example:
Task Guardrails
Task guardrails provide a way to validate and transform task outputs before they are passed to the next task. This feature helps ensure data quality and provides efeedback to agents when their output doesn’t meet specific criteria.
Using Task Guardrails
To add a guardrail to a task, provide a validation function through the guardrail
parameter:
Guardrail Function Requirements
-
Function Signature:
- Must accept exactly one parameter (the task output)
- Should return a tuple of
(bool, Any)
- Type hints are recommended but optional
-
Return Values:
- Success: Return
(True, validated_result)
- Failure: Return
(False, error_details)
- Success: Return
Error Handling Best Practices
- Structured Error Responses:
-
Error Categories:
- Use specific error codes
- Include relevant context
- Provide actionable feedback
-
Validation Chain:
Handling Guardrail Results
When a guardrail returns (False, error)
:
- The error is sent back to the agent
- The agent attempts to fix the issue
- The process repeats until:
- The guardrail returns
(True, result)
- Maximum retries are reached
- The guardrail returns
Example with retry handling:
Getting Structured Consistent Outputs from Tasks
It’s also important to note that the output of the final task of a crew becomes the final output of the actual crew itself.
Using output_pydantic
The 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:
In this example:
- A Pydantic model Blog is defined with title and content fields.
- The task task1 uses the output_pydantic property to specify that its output should conform to the Blog model.
- After executing the crew, you can access the structured output in multiple ways as shown.
Explanation of Accessing the Output
- Dictionary-Style Indexing: You can directly access the fields using result[“field_name”]. This works because the CrewOutput class implements the getitem method.
- Directly from Pydantic Model: Access the attributes directly from the result.pydantic object.
- Using to_dict() Method: Convert the output to a dictionary and access the fields.
- Printing the Entire Object: Simply print the result object to see the structured output.
Using output_json
The 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
:
In this example:
- A Pydantic model Blog is defined with title and content fields, which is used to specify the structure of the JSON output.
- The task task1 uses the output_json property to indicate that it expects a JSON output conforming to the Blog model.
- After executing the crew, you can access the structured JSON output in two ways as shown.
Explanation of Accessing the Output
- Accessing Properties Using Dictionary-Style Indexing: You can access the fields directly using result[“field_name”]. This is possible because the CrewOutput class implements the getitem method, allowing you to treat the output like a dictionary. In this option, we’re retrieving the title and content from the result.
- Printing the Entire Blog Object: By printing result, you get the string representation of the CrewOutput object. Since the str method is implemented to return the JSON output, this will display the entire output as a formatted string representing the Blog object.
By using output_pydantic or output_json, you ensure that your tasks produce outputs in a consistent and structured format, making it easier to process and utilize the data within your application or across multiple tasks.
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.
Task Guardrails
Task guardrails provide a powerful way to validate, transform, or filter task outputs before they are passed to the next task. Guardrails are optional functions that execute before the next task starts, allowing you to ensure that task outputs meet specific requirements or formats.
Basic Usage
How Guardrails Work
- Optional Attribute: Guardrails are an optional attribute at the task level, allowing you to add validation only where needed.
- Execution Timing: The guardrail function is executed before the next task starts, ensuring valid data flow between tasks.
- Return Format: Guardrails must return a tuple of
(success, data)
:- If
success
isTrue
,data
is the validated/transformed result - If
success
isFalse
,data
is the error message
- If
- Result Routing:
- On success (
True
), the result is automatically passed to the next task - On failure (
False
), the error is sent back to the agent to generate a new answer
- On success (
Common Use Cases
Data Format Validation
Content Filtering
Data Transformation
Advanced Features
Chaining Multiple Validations
Custom Retry Logic
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.