Tasks are units of work that agents execute. Each task has a description, expected output, and optional configuration for routing, retries, and advanced features.
Quick Start
from praisonaiagents import Agent , Task , AgentTeam
agent = Agent ( name = " Researcher " , instructions = " Research topics " )
task = Task (
action = " Research AI trends in 2024 " ,
expected_output = " A summary report " ,
agent = agent
)
team = AgentTeam ( agents =[ agent ], tasks =[ task ])
team . start ()
# Custom function instead of agent
def process_data ( context ):
return { " result " : " Processed " }
task = Task (
action = " Process the data " ,
handler = process_data
)
Core Parameters
Parameter Type Description actionstrWhat the task should accomplish expected_outputstrWhat the output should look like agentAgentAgent to execute the task namestrOptional identifier toolslistTools available to the task
description is deprecated. Use action instead.
Task Dependencies
Use context to chain tasks:
task1 = Task (
name = " research " ,
action = " Research AI trends " ,
agent = researcher
)
task2 = Task (
name = " write " ,
action = " Write a report based on research " ,
agent = writer ,
context =[ task1 ] # or depends_on=[task1]
)
Conditional Execution
Simple Condition (should_run)
task = Task (
action = " Optional step " ,
should_run = lambda ctx : ctx . get ( " enabled " , False )
)
When/Then/Else Routing
task = Task (
action = " Evaluate score " ,
when = " {{ score }} > 80 " , # Condition expression
then_task = " celebrate " , # If true
else_task = " retry " # If false
)
Loop Support
Iterate over a list:
task = Task (
action = " Process {{ item }} " ,
loop_over = " items " , # Variable containing the list
loop_var = " item " # Name for each item
)
Callbacks
Use on_task_complete for task completion notifications:
def on_complete ( output ):
print ( f "Task done: { output . raw } " )
task = Task (
action = " Do something " ,
agent = agent ,
on_task_complete = on_complete
)
The callback parameter is deprecated. Use on_task_complete instead.
Guardrails
Validate task output before accepting:
def validate_output ( output ):
if len ( output . raw ) < 100 :
return ( False , " Output too short " )
return ( True , output )
task = Task (
action = " Write detailed report " ,
agent = writer ,
guardrails = validate_output ,
max_retries = 3
)
Robustness Features
Parameter Type Description max_retriesintMaximum retry attempts (default: 3) retry_delayfloatSeconds between retries skip_on_failureboolContinue workflow if task fails
task = Task (
action = " Optional enrichment " ,
skip_on_failure = True , # Workflow continues if this fails
retry_delay = 1.0 , # Wait between retries
max_retries = 3
)
Output Configuration
Parameter Type Description output_filestrSave output to file output_jsonBaseModelParse output as JSON output_pydanticBaseModelValidate with Pydantic output_variablestrStore output in workflow variable
from pydantic import BaseModel
class Report ( BaseModel ):
title : str
summary : str
task = Task (
action = " Generate report " ,
output_pydantic = Report ,
output_file = " report.json "
)
Feature Configs
Enable advanced features per-task:
task = Task (
action = " Complex task " ,
autonomy = True , # Agent autonomy level
knowledge =[ " docs/ " ], # Knowledge base paths
web = True , # Web search enabled
reflection = True , # Self-reflection
planning = True , # Task planning
caching = True # Response caching
)
Task Types
Type Description taskStandard task (default) decisionBranching logic with conditions loopIterate over items
# Decision task
task = Task (
action = " Decide next step " ,
task_type = " decision " ,
condition ={
" approve " : [ " next_task " ],
" reject " : [ " error_task " ]
}
)
Async Execution
task = Task (
action = " Long running task " ,
agent = agent ,
async_execution = True
)
# Run async
result = await team . astart ()
Full Parameter Reference
Parameter Type Default Description actionstr- What the task should do (required) expected_outputstr”Complete successfully” Expected output format agentAgentNone Agent to execute namestrNone Task identifier toolslist[] Available tools contextlist[] Dependent tasks depends_onlist[] Alias for context handlerCallableNone Custom function should_runCallableNone Condition to run loop_overstrNone Variable containing list loop_varstr”item” Loop variable name whenstrNone Condition expression then_taskstrNone Task if condition true else_taskstrNone Task if condition false on_task_completeCallableNone Completion callback guardrailsCallable/strNone Output validation max_retriesint3 Max retry attempts retry_delayfloat0.0 Retry delay seconds skip_on_failureboolFalse Continue on failure async_executionboolFalse Run async output_filestrNone Save to file output_jsonBaseModelNone JSON output model output_pydanticBaseModelNone Pydantic validation output_variablestrNone Workflow variable name task_typestr”task” task/decision/loop routingdictNext task routing imageslist[] Image inputs input_filestrNone Input file path memoryMemoryNone Memory instance variablesdictTask variables descriptionstr- Deprecated - use action