Kubeflow Pipelines Server API
The `kfp-server-api` library provides a Python client for interacting directly with the Kubeflow Pipelines (KFP) v2 server API. It offers low-level access to KFP resources like pipelines, runs, and experiments, suitable for advanced integrations or custom automation. It is part of the KFP SDK v2 ecosystem, currently at version 2.16.0, and typically released in lockstep with the main `kfp` SDK.
Warnings
- breaking The `kfp-server-api` library is built for Kubeflow Pipelines v2. Code written for KFP v1 client libraries (e.g., `kubeflow-pipelines` or older `kfp` versions) will likely be incompatible due to significant changes in API contracts, models, and service endpoints.
- gotcha Direct usage of `kfp-server-api` requires explicit management of authentication and API endpoints. Unlike the higher-level `kfp` SDK, which often infers these from the environment or provides simpler `Client` objects, you must manually configure bearer tokens, cookies, or other authentication methods.
- gotcha For stability, it is crucial to ensure that the `kfp-server-api` version aligns with the version of the `kfp` SDK and the KFP backend server you are interacting with. Mismatched versions can lead to deserialization errors, unexpected API behavior, or missing features.
Install
-
pip install kfp-server-api==2.16.0 -
pip install kfp # This will pull in kfp-server-api as a dependency
Imports
- Configuration
from kfp_server_api import Configuration
- ApiClient
from kfp_server_api import ApiClient
- PipelineServiceApi
from kfp_server_api.api.pipeline_service_api import PipelineServiceApi
- RunServiceApi
from kfp_server_api.api.run_service_api import RunServiceApi
- ExperimentServiceApi
from kfp_server_api.api.experiment_service_api import ExperimentServiceApi
- V2beta1Pipeline
from kfp_server_api.models import V2beta1Pipeline
Quickstart
import os
from kfp_server_api import Configuration, ApiClient
from kfp_server_api.api.pipeline_service_api import PipelineServiceApi
# Replace with your KFP API endpoint. E.g., 'http://localhost:8080/pipeline'
# or 'https://<your-kfp-host>/pipeline'
KFP_HOST = os.environ.get('KFP_SERVER_API_HOST', 'http://localhost:8080')
# Configure API key authorization: Bearer
# For most KFP deployments, you might need a service account token or session cookie.
# Example for no auth or basic local setup:
configuration = Configuration(host=KFP_HOST)
# For token-based auth (e.g., from a service account or user session):
# KFP_API_TOKEN = os.environ.get('KFP_SERVER_API_TOKEN')
# if KFP_API_TOKEN:
# configuration.access_token = KFP_API_TOKEN
# Create an API client
with ApiClient(configuration) as api_client:
# Create an instance of the PipelineServiceApi
pipeline_api = PipelineServiceApi(api_client)
try:
# List pipelines (requires appropriate authentication and permissions)
list_response = pipeline_api.list_pipelines()
print(f"Successfully connected to KFP API host: {KFP_HOST}")
print(f"Found {len(list_response.pipelines or [])} pipelines.")
for p in (list_response.pipelines or []):
print(f" - {p.display_name} (ID: {p.pipeline_id})")
except Exception as e:
print(f"Error connecting to KFP API or listing pipelines: {e}")
print("Make sure KFP_SERVER_API_HOST is correctly set and you have necessary authentication.")