Kubeflow Pipelines SDK

2.16.0 · active · verified Wed Apr 08

The Kubeflow Pipelines SDK (kfp), currently at version 2.16.0, is a Python library for building and deploying portable, scalable machine learning workflows based on Docker containers within the Kubeflow project. It allows users to compose multi-step workflows (pipelines) as a graph of containerized tasks using Python code and/or YAML. Releases are frequent, often bundling the SDK with related components like `kfp-pipeline-spec`, `kfp-server-api`, and `kfp-kubernetes`.

Warnings

Install

Imports

Quickstart

This quickstart defines a simple Python function as a KFP component using `@dsl.component` and then orchestrates it into a pipeline using `@dsl.pipeline`. It demonstrates the basic structure for authoring KFP v2 pipelines. To actually execute the pipeline, it needs to be compiled and submitted to a running Kubeflow Pipelines backend.

import kfp
from kfp import dsl
import os

# Define a lightweight Python component
@dsl.component
def add(a: float, b: float) -> float:
    '''Calculates sum of two arguments'''
    return a + b

# Define a pipeline using the component
@dsl.pipeline(
    name='Addition pipeline',
    description='An example pipeline that performs addition calculations.'
)
def add_pipeline(
    a: float = 1.0,
    b: float = 7.0,
):
    first_add_task = add(a=a, b=4.0)
    second_add_task = add(a=first_add_task.output, b=b)

# --- Running the pipeline (requires KFP backend) ---
# In a real environment, you'd configure the KFP client to connect to your KFP instance.
# For local testing without a KFP backend, you can use `kfp.local.init`.

# Example of compiling a pipeline (no KFP backend needed for this step)
# compiler = kfp.compiler.Compiler()
# compiler.compile(pipeline_func=add_pipeline, package_path='add_pipeline.yaml')

# Example of running a pipeline against a KFP endpoint
# client = kfp.Client(host=os.environ.get('KFP_HOST', 'http://localhost:8080'))
# run = client.create_run_from_pipeline_func(
#     add_pipeline,
#     arguments={'a': 7.0, 'b': 8.0}
# )
# print(f"Pipeline run initiated: {run.url}")

print("Pipeline 'add_pipeline' defined successfully. To run, compile and submit to a KFP backend.")

view raw JSON →