Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Kubeflow Pipelines Dsl Condition

From Leeroopedia
Sources Domains Last Updated
Kubeflow Pipelines, KFP Control Flow ML_Pipelines, Control_Flow 2026-02-13

Overview

Concrete tool for conditional task execution in pipelines provided by the KFP DSL context manager.

Description

dsl.Condition is a Python context manager that gates execution of contained tasks. It takes a boolean expression comparing a PipelineChannel (task output) against a value. All tasks inside the with block only execute when the condition is true.

Usage

Use within a @dsl.pipeline function to create conditional branches based on task outputs.

Code Reference

Source Location: Repository: kubeflow/pipelines, Files: samples/core/condition/condition_v2.py (L39-47), samples/core/condition/nested_condition.py (L32-45)

Signature:

class Condition:
    def __init__(
        self,
        condition: PipelineChannel,  # Boolean expression: task.output == value
        name: Optional[str] = None,
    ): ...
    def __enter__(self) -> 'Condition': ...
    def __exit__(self, *args): ...

Import: from kfp import dsl

I/O Contract

Inputs
Parameter Type Required Description
condition PipelineChannel boolean expression Yes Comparison of task output to value
name str No Optional display name
Outputs
Output Description
ConditionContext scope All tasks defined within the with block execute conditionally

Usage Examples

Example 1: Basic condition (condition_v2.py)

@dsl.pipeline(name='condition-v2')
def condition_pipeline(force_flip_result: str = ''):
    flip1 = flip_coin(force_flip_result=force_flip_result)

    with dsl.Condition(flip1.output == 'heads'):
        flip2 = flip_coin()
        print_msg(msg=flip2.output)

Example 2: Nested conditions (nested_condition.py)

with dsl.Condition(flip1.output != 'no-such-result'):
    flip2 = flip_coin()
    flip3 = flip_coin()
    with dsl.Condition(flip2.output == flip3.output):
        print_msg(msg='Both flips matched!')

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment