The conditional feature in steps allows you to control step execution based on specified conditions. When a condition is defined, the step will only execute if the condition evaluates to true. If the condition evaluates to false, the step will be skipped with a status of ‘SKIPPED’.

Enabling Conditional

To enable conditional for a step:

  1. Click the step’s options menu (⋮)
  2. Click Add Conditional
  3. Enter a condition in the Conditional Expression field that will determine whether the step should run

Conditional Expression

  • A conditional is a JavaScript expression that evaluates to a boolean value
  • Defined using the conditional property in step configuration
  • Must return true or false
  • Cannot contain async operations

Expression Rules

Conditional expressions must follow specific rules to ensure proper execution.

Syntax Requirements

return CODE_VALIDATE.output.is_valid === true; // Boolean expression

Valid Conditional Patterns

// Simple comparison
return input.document_type === 'pdf';

// Logical operators
return CODE_STEP_1.output.score > 0.8 && CODE_STEP_2.output.verified;

// Existence checks
return CODE_STEP_1.output.results?.length > 0;

// Multiple conditions
return (
  input.process_images === true && MODEL_STEP_1.output.message.confidence > 0.9
);

Invalid Patterns

// WRONG - Async operations
const result = await checkCondition();
return result;

// WRONG - Function declarations
function check() {
  return true;
}

// WRONG - Complex logic blocks
if (condition) {
  return true;
} else {
  return false;
}

Common Conditional Patterns

Here are frequently used patterns for writing conditional expressions in workflows. These patterns demonstrate best practices for handling different types of checks and comparisons.

Input-Based Conditions

// Check input field value
return input.file_type === 'pdf';

// Check multiple input conditions
return input.process_images === true && input.image_count > 0;

Previous Step Output Conditions

// Check model analysis results
return MODEL_ANALYZE.output.message.confidence >= 0.9;

// Check multiple step outputs
return (
  CODE_VALIDATE.output.is_valid && MODEL_PRIMARY.output.message.requires_review
);

Array and Object Handling

// Check array length
return CODE_EXTRACT.output.items?.length > 0;

// Check object properties
return (
  CODE_EXTRACT.output.metadata?.type === 'financial' &&
  CODE_EXTRACT.output.metadata?.year === 2023
);

Error Handling

Here are some best practices for error handling in conditional expressions.

Conditional Evaluation Errors

// GOOD - Safe property access with default
return CODE_STEP.output?.score ?? 0 > 0.5;

// GOOD - Existence check
return CODE_STEP.output && CODE_STEP.output.score > 0.5;

Step Skip Handling

When a step is skipped due to a conditional evaluating to false, subsequent steps need to handle this gracefully when accessing the output of the skipped step. The following example shows how to properly check for and handle skipped steps in your workflow:

CODE_HANDLE_RESULTS
// Handle potential skipped step
// Check if previous step was skipped
if (!MODEL_ANALYZE.output) {
  return { status: 'skipped', message: 'Analysis step was skipped' };
}
// Process results
return { status: 'completed', results: MODEL_ANALYZE.output.message };

Common Issues and Solutions

Here are some common issues you might encounter when writing conditionals and their recommended solutions:

  1. Undefined Property Access
// Problem
return CODE_STEP.output.results.length > 0;

// Solution
return CODE_STEP.output?.results?.length > 0;
  1. Invalid Boolean Expressions
// Problem
input.process_type === 'full';

// Solution: Use return statement
return input.process_type === 'full';
  1. Complex Logic
// Problem
if (CODE_STEP.output.score > 0.5) {
  return true;
}

// Solution
return CODE_STEP.output.score > 0.5;

Examples

Below are practical examples demonstrating how to effectively use conditionals in different workflow scenarios. These examples showcase proper syntax, best practices, and common use cases for conditional step execution.

Document Processing Workflow

This example demonstrates a document processing workflow that uses conditionals to control the execution flow:

  1. A PDF document is uploaded via a dropzone input
  2. The CODE_VALIDATE_INPUT step validates that it’s a PDF file and checks its size
  3. The MODEL_ANALYZE step only runs if validation passes and file is under 10MB
  4. The CODE_PROCESS_RESULTS step processes the analysis results if they exist
  5. Finally, a simple code step demonstrates the end of the workflow