PraisonAI now provides a unified condition syntax that works the same way in both
AgentFlow (pipelines) and Task (multi-agent teams).Overview
Conditional execution allows you to control workflow branching based on variables, scores, or other runtime values. PraisonAI supports:- String expression conditions - Simple
{{variable}}syntax for comparisons - Dictionary routing - Map decision values to next tasks
- Callable conditions - Custom Python functions
Quick Start
Condition Syntax
String Expression Conditions
Use{{variable}} placeholders with comparison operators:
| Operator | Example | Description |
|---|---|---|
> | {{score}} > 80 | Greater than |
>= | {{score}} >= 80 | Greater than or equal |
< | {{score}} < 50 | Less than |
<= | {{score}} <= 50 | Less than or equal |
== | {{status}} == approved | Equal to |
!= | {{status}} != rejected | Not equal to |
in | {{word}} in {{text}} | Contains (substring) |
contains | {{list}} contains {{item}} | Contains (list) |
Examples
Task Condition Parameters
when Parameter
The when parameter accepts a string expression condition:
then_task and else_task
Route to different tasks based on condition result:
routing Parameter (Advanced)
For LLM-driven decisions, use the routing parameter (formerly condition):
The
condition parameter still works for backward compatibility, but routing is preferred for clarity.should_run Callable
For complex logic, use a callable:
AgentFlow Conditions
when() Function
Nested Conditions
Flow Diagram
Best Practices
Use simple conditions
Use simple conditions
Keep conditions readable and simple. Complex logic should go in
should_run callables.Provide both then_task and else_task
Provide both then_task and else_task
Always specify both branches to make the flow explicit:
Use routing for LLM decisions
Use routing for LLM decisions
When the LLM needs to make a decision, use
routing with task_type="decision":Migration Guide
From condition to routing
Adding when to existing Tasks
API Reference
Task Parameters
| Parameter | Type | Description |
|---|---|---|
when | str | String expression condition |
then_task | str | Task name to run if condition is True |
else_task | str | Task name to run if condition is False |
routing | Dict[str, List[str]] | Map decision values to task names |
should_run | Callable | Custom condition function |
Task Methods
| Method | Returns | Description |
|---|---|---|
evaluate_when(context) | bool | Evaluate the when condition |
get_next_task(context) | str | Get next task based on condition |

