Skip to main content
Conditional execution gates tasks and workflow steps on runtime values using one when syntax that works in both AgentFlow pipelines and Task teams.
The user defines workflows; when expressions gate tasks and AgentFlow steps on runtime variables.

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:
String comparisons don’t require quotes: {{status}} == approved works correctly.

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

How It Works


Best Practices

Keep conditions readable and simple. Complex logic should go in should_run callables.
Always specify both branches to make the flow explicit:
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

AgentFlow

Learn about deterministic pipelines

AgentTeam

Multi-agent task orchestration