Back to Step Functions

handleConditional

Advancedcontrol_flow

Enable dynamic workflow routing based on conditions, data comparisons, and logical evaluations

overview

The handleConditional function enables dynamic workflow routing based on conditions, data comparisons, and logical evaluations. This powerful function allows your Vibes to make intelligent decisions and branch into different execution paths based on the data processed in previous steps.

Key Capabilities

  • Implement if/then/else logic in workflows
  • Route workflow execution to different steps based on conditions
  • Evaluate data from previous steps to make decisions
  • Handle complex multi-condition scenarios with logical operators
  • Support switch-case style conditional routing

Function Structure

If Type Structure

- id: check_condition
  function: handleConditional
  input:
    condition:
      type: "if"
      condition:
        operator: "eq"
        left: "${steps.status.output.value}"
        right: "active"
      then: "success_step"
      else: "failure_step"

Implements simple if/then/else logic with a single condition evaluation

Switch Type Structure

- id: route_by_category
  function: handleConditional
  input:
    condition:
      type: "switch"
      expression: "${steps.extract.output.category}"
      cases:
        - value: "urgent"
          stepId: "urgent_handler"
        - value: "normal"
          stepId: "normal_handler"
      default: "default_handler"

Implements switch-case style routing with multiple possible outcomes

Conditional Operators

Comparison Operators

eq

Tests if two values are exactly equal

operator: "eq"
left: "${steps.status_check.output.status}"
right: "completed"
ne

Tests if two values are not equal

operator: "ne"
left: "${steps.result.output.error_count}"
right: 0
gt

Tests if left value is numerically greater than right value

operator: "gt"
left: "${steps.calculate_total.output.amount}"
right: 1000
gte

Tests if left value is greater than or equal to right value

operator: "gte"
left: "${steps.user_age.output.age}"
right: 18
lt

Tests if left value is numerically less than right value

operator: "lt"
left: "${steps.inventory.output.stock_level}"
right: 10
lte

Tests if left value is less than or equal to right value

operator: "lte"
left: "${steps.score.output.points}"
right: 50

String Operators

contains

Tests if left string contains the right string

operator: "contains"
left: "${steps.error_message.output.message}"
right: "timeout"
startsWith

Tests if left string starts with the right string

operator: "startsWith"
left: "${steps.user_email.output.email}"
right: "admin"
endsWith

Tests if left string ends with the right string

operator: "endsWith"
left: "${steps.filename.output.name}"
right: ".pdf"
regex

Tests if left string matches the right regular expression pattern

operator: "regex"
left: "${steps.phone_number.output.number}"
right: "^\\+1\\d{10}$"

Existence Operators

exists

Tests if the value exists (is not null or undefined)

operator: "exists"
left: "${steps.user_profile.output.avatar_url}"
isEmpty

Tests if the value is empty (null, undefined, empty string, empty array, or empty object)

operator: "isEmpty"
left: "${steps.search_results.output.results}"
isNotEmpty

Tests if the value is not empty

operator: "isNotEmpty"
left: "${steps.validation_errors.output.errors}"

Logical Operators

and

Tests if all conditions in the array are true

operator: "and"
conditions:
  - operator: "eq"
    left: "${steps.status.output.active}"
    right: true
  - operator: "gt"
    left: "${steps.balance.output.amount}"
    right: 0
or

Tests if any condition in the array is true

operator: "or"
conditions:
  - operator: "eq"
    left: "${steps.payment.output.method}"
    right: "credit_card"
  - operator: "eq"
    left: "${steps.payment.output.method}"
    right: "paypal"
in

Tests if left value exists in the right array

operator: "in"
left: "${steps.user_role.output.role}"
right: ["admin", "moderator", "super_user"]

Switch Case Types

Exact Value Matching

Matches exact values (default behavior when no type is specified)

cases:
  - value: "premium"
    stepId: "premium_handler"
  - value: "basic"
    stepId: "basic_handler"

Range Matching

Matches numeric values within a specified range

cases:
  - type: "range"
    min: 0
    max: 10
    stepId: "low_range_handler"
  - type: "range"
    min: 11
    max: 100
    stepId: "medium_range_handler"

Pattern Matching

Matches string values using regular expressions

cases:
  - type: "pattern"
    pattern: "user_.*"
    stepId: "user_handler"
  - type: "pattern"
    pattern: "admin_.*"
    stepId: "admin_handler"

Contains Matching

Matches if the expression contains the specified substring

cases:
  - type: "contains"
    value: "error"
    stepId: "error_handler"
  - type: "contains"
    value: "warning"
    stepId: "warning_handler"

Practical Examples

Simple If Example

Check user count and route to appropriate processing step

- id: check_user_count
  function: handleConditional
  input:
    condition:
      type: "if"
      condition:
        operator: "gt"
        left: "${steps.fetch_users.output.count}"
        right: 100
      then: "process_large_dataset"
      else: "process_small_dataset"

Complex Logical Conditions

Combine multiple conditions using AND logic to determine user flow

- id: check_multiple_conditions
  function: handleConditional
  input:
    condition:
      type: "if"
      condition:
        operator: "and"
        conditions:
          - operator: "eq"
            left: "${steps.user_data.output.status}"
            right: "active"
          - operator: "gt"
            left: "${steps.user_data.output.score}"
            right: 80
      then: "premium_user_flow"
      else: "standard_user_flow"

Switch Case Example

Route workflow based on priority level using switch-case logic

- id: route_by_priority
  function: handleConditional
  input:
    condition:
      type: "switch"
      expression: "${steps.extract_priority.output.priority_level}"
      cases:
        - value: "high"
          stepId: "urgent_processing"
        - value: "medium"
          stepId: "standard_processing"
        - value: "low"
          stepId: "batch_processing"
      default: "error_handler"

Output Format

The queryKnowledgebase function returns structured document entries with citation information and content excerpts.

{
  "next_step_id": "target_step_id",
  "conditionResult": {
    "type": "if" | "switch",
    "evaluatedTo": "target_step_id",
    "condition": { /* original condition object */ }
  }
}

Variable References

Supported References

${steps.previous_step.output.property_name}

Reference output from previous steps

${global_variable_name}

Reference global variables

${steps.data.output.user.profile.email}

Access nested object properties

${steps.results.output.items[0].status}

Access specific array elements

Reference Tips

  • Use descriptive step IDs for clarity
  • Validate that referenced data exists
  • Handle null or undefined values gracefully
  • Test all conditional branches thoroughly

Common Use Cases

Business Process Routing

Direct workflows based on business rules and user roles

  • Route support tickets based on priority and category
  • Direct users through different onboarding flows
  • Assign tasks based on user permissions and workload

Data Processing Decisions

Choose processing paths based on data characteristics

  • Choose processing algorithms based on data size
  • Route data through different validation pipelines
  • Select appropriate output formats based on user preferences

Error Handling and Recovery

Implement sophisticated error handling and recovery logic

  • Implement retry logic with condition-based limits
  • Route failed processes to manual review queues
  • Escalate issues based on severity levels

Quality Control

Implement quality gates and approval processes

  • Implement multi-stage approval processes
  • Route content based on quality scores
  • Apply different validation rules based on content type

Best Practices

Condition Design

  • Write clear, readable conditions
  • Use logical grouping for complex conditions
  • Always provide appropriate default paths

Error Prevention

  • Validate step outputs before using in conditions
  • Handle edge cases and unexpected values
  • Test all conditional branches thoroughly