when syntax that works in both AgentFlow pipelines and Task teams.
Runtime support:
Task(when=..., then_task=..., else_task=...) and Task(routing=...) are wired into the markdown workflow engine (WorkflowManager, .praisonai/workflows/*.md). Run them with praisonai workflow run <file>.md or WorkflowManager.execute(...) — the runtime that honours these fields. See Markdown Workflow Branches for a runnable branch example.when expressions gate tasks and AgentFlow steps on runtime variables.
Since PraisonAI PR #4020,
when/then_task/else_task routing is wired into PraisonAIAgents. On earlier releases the API existed on Task but the Process orchestrator never consulted it — when-only tasks silently stalled or fell through to unrelated tasks.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
1
Task or AgentFlow
Condition Syntax
String Expression Conditions
Use{{variable}} placeholders with comparison operators:
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). Pass a bare dict, or the TaskRoutingConfig dataclass for clarity:
TaskRoutingConfig(...) unpacks onto Task.branch_condition and Task.next_steps — the attributes the executor reads — so Task.condition stays a plain dict/str.Precedence Ladder
Therouting parameter resolves in this order (only the last two forms are supported today):
Bool > String > Dict > Config — the markdown engine honours the Dict and Config forms.
The
condition parameter still works for backward compatibility, but routing is preferred for clarity.End-to-End Markdown Example
A branch actually being taken in a markdown workflow:classify_and_route.md
step1’s output contains success, execution jumps to handle_success, skipping handle_failure.
should_run Callable
For complex logic, use a callable:
AgentFlow Conditions
when() Function
Nested Conditions
Multi-agent Workflows (PraisonAIAgents)
when/then_task/else_task also routes multi-agent workflows built with PraisonAIAgents.
How the routing context is built — the
when expression is evaluated against the current task (the one that just completed), not the target:- The context is
result.to_dict()(fromjson_dict/pydantic) merged withprevious_output = result.raw. - Access structured fields as
{{field_name}}(e.g.{{score}}when the agent returns{"score": 90}). - Access raw text as
{{previous_output}}.
Priority — when both
when and next_tasks are set on the same task, when-routing wins.Clean termination — if the taken branch resolves to
None (e.g. only then_task is set and the condition is false, with no next_tasks fallback), the workflow ends cleanly on that path. It does not pick an unrelated not-started task.Flow Diagram
How It Works
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
Specify both branches when you want an explicit fork:
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
Task Methods
Related
Markdown Workflow Branches
Route markdown-workflow steps on output
Tasks
Task fields including when / then_task / else_task
AgentFlow
Learn about deterministic pipelines
AgentTeam
Multi-agent task orchestration

