SkillByAIOpen interactive version →

Lesson 10 / 25

If/Else Branches

Deterministic routing on variables.

Conditions, first match wins

The if/else node checks conditions on variables (equals, contains, greater than, is empty, combined with AND/OR) and sends the run down the first matching branch, with an else branch for everything else. Use it when the decision depends on structured values: a category, an amount, a plan type, whether retrieval found anything. Order branches from most specific to most general, and make sure the else branch does something sensible. Dify's node names, menus and options change between versions; check the current Dify documentation.

Control the path through the graph

Logic nodes decide which branch runs and repeat steps over lists.

Figure 4.1 — If/else, classifier, iteration and aggregation.

A routing rule set, run

I ran this with plain Python 3 (scikit-learn 1.9.1 where imported). It models or tests one piece of a Dify app locally; Dify itself was not running. A model of an if/else node: a refund over 5,000 goes to human review, other refunds and billing go to the billing agent, technical questions from enterprise customers to priority support, and everything else falls through to the general answer branch.

# Simplified model of an if/else node: conditions over upstream variables, first match wins
def if_else(v):
    if v["category"] == "refund" and v["amount"] > 5000:
        return "human_review"
    if v["category"] in ("refund", "billing"):
        return "billing_agent"
    if v["category"] == "technical" and v["plan"] == "enterprise":
        return "priority_support"
    return "general_answer"   # the ELSE branch
for case in [{"category": "refund", "amount": 8000, "plan": "basic"},
             {"category": "billing", "amount": 0, "plan": "basic"},
             {"category": "technical", "amount": 0, "plan": "enterprise"},
             {"category": "other", "amount": 0, "plan": "basic"}]:
    print(case["category"], case["amount"], case["plan"], "->", if_else(case))

Output:

refund 8000 basic -> human_review
billing 0 basic -> billing_agent
technical 0 enterprise -> priority_support
other 0 basic -> general_answer

Route on structured fields

Have an earlier node produce a category or number, then branch on that; branching on raw free text is fragile.

Quick check: Why order if/else conditions from specific to general?

  • The first matching branch wins, so general conditions would catch specific cases
  • Dify sorts them alphabetically anyway
  • Order never matters
  • Only one condition is allowed
Answer

The first matching branch wins, so general conditions would catch specific cases — Specific rules first.