Tasks are the fundamental units of work in PraisonAI, defining what needs to be done, how it should be done, and who should do it.

Understanding Tasks

Task Definition

Description & Requirements

Agent Assignment

Matching & Delegation

Execution

Processing & Tools

Output

Results & Validation

Pending
In Progress
Completed
Failed

Core Components

Task Definition

Clear description of work to be done and expected outputs

Agent Assignment

Matching tasks with capable agents

Tool Access

Resources available for task completion

Output Handling

Managing and formatting task results

Task Configuration

Task Types

1

Basic Task

Simple, single-operation tasks with clear inputs and outputs

2

Decision Task

Tasks involving choices and conditional paths

decision_task = Task(
    type="decision",
    conditions={
        "success": ["next_task"],
        "failure": ["error_task"]
    }
)
3

Loop Task

Repetitive operations over collections

loop_task = Task(
    type="loop",
    items=data_list,
    operation="process_item"
)

Task Relationships

Properly managing task dependencies is crucial for complex workflows. Always ensure proper context sharing and error handling.

Context Sharing

task_a = Task(name="research")
task_b = Task(
    name="analyze",
    context=[task_a]  # Uses task_a's output
)

Task Dependencies

RelationshipDescriptionExample
SequentialTasks run one after anotherResearch → Analysis → Report
ParallelIndependent tasks run simultaneouslyData Collection + Processing
ConditionalTasks depend on previous resultsSuccess → Next Task, Failure → Retry

Advanced Features

Output Handling

  • Multiple output formats
  • Structured data validation
  • File system integration

Task Control

  • Execution flow control
  • Error recovery
  • Progress tracking

Best Practices

Design tasks to be atomic and focused. Each task should have a single, clear responsibility.

  1. Clear task descriptions
  2. Well-defined success criteria
  3. Appropriate tool selection
  4. Error handling consideration
  5. Resource management

Next Steps

Async Task Execution

Tasks can be executed asynchronously by setting async_execution=True. This is particularly useful for tasks that involve I/O operations or when you want to run multiple tasks in parallel.

Async Task Configuration

async_task = Task(
    description="Perform async operation",
    expected_output="Async result",
    agent=async_agent,
    async_execution=True,  # Enable async execution
    callback=async_callback  # Optional async callback
)

Async Callbacks

Tasks support both synchronous and asynchronous callbacks:

async def async_callback(output: TaskOutput):
    await asyncio.sleep(1)  # Simulate async operation
    print(f"Processed output: {output.raw}")

# Task with async callback
task = Task(
    description="Task with async callback",
    expected_output="Result",
    agent=agent,
    async_execution=True,
    callback=async_callback
)

Mixing Sync and Async Tasks

You can mix synchronous and asynchronous tasks in the same workflow:

# Sync task
sync_task = Task(
    description="Synchronous operation",
    expected_output="Sync result",
    agent=sync_agent,
    async_execution=False
)

# Async task
async_task = Task(
    description="Asynchronous operation",
    expected_output="Async result",
    agent=async_agent,
    async_execution=True
)

# Create agents with mixed tasks
agents = PraisonAIAgents(
    agents=[sync_agent, async_agent],
    tasks=[sync_task, async_task]
)

# Run asynchronously
result = await agents.astart()

Was this page helpful?