A data scientist creates a custom Python function component for a Vertex AI pipeline using the Kubeflow Pipelines SDK v2. The component takes a string parameter 'input_text' and outputs a Metrics artifact. The scientist wants to include a lightweight Python function without building a container. Which code snippet correctly defines this component?
Trap 1: @dsl.component\ndef my_component(input_text: str) -> Metric:\n…
Incorrect artifact type: should be Metrics (plural) and base_image missing; also Metric class doesn't exist in kfp.dsl.
Trap 2: @dsl.pipeline\ndef my_pipeline(input_text: str):\n metrics =…
Incorrect: @dsl.pipeline is for pipeline definitions, not components. Also missing return type.
Trap 3: def my_component(input_text: str) -> Metrics:\n from kfp.dsl…
Missing @dsl.component decorator; without it, this is a plain Python function, not a KFP component.
- A
@dsl.component\ndef my_component(input_text: str) -> Metric:\n metrics = Metric()\n metrics.log_metric('length', len(input_text))
Why it fails: Incorrect artifact type: should be Metrics (plural) and base_image missing; also Metric class doesn't exist in kfp.dsl.
- B
@dsl.pipeline\ndef my_pipeline(input_text: str):\n metrics = Metrics()\n metrics.log_metric('length', len(input_text))
Why it fails: Incorrect: @dsl.pipeline is for pipeline definitions, not components. Also missing return type.
- C
def my_component(input_text: str) -> Metrics:\n from kfp.dsl import Metrics\n metrics = Metrics()\n metrics.log_metric('length', len(input_text))\n return metrics
Why it fails: Missing @dsl.component decorator; without it, this is a plain Python function, not a KFP component.
- D
@dsl.component(base_image='python:3.9')\ndef my_component(input_text: str) -> Metrics:\n from kfp.dsl import Metrics\n metrics = Metrics()\n metrics.log_metric('length', len(input_text))\n return metrics
The @dsl.component decorator with base_image compiles a lightweight Python function into a component without a custom container build, satisfying the no-container constraint. The Metrics output annotation and log_metric call correctly produce the required artifact, matching the Kubeflow Pipelines SDK v2 signature.