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 wrong: 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 wrong: 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 wrong: 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
Correct: Uses @dsl.component with base_image, imports Metrics inside the function, and returns a Metrics artifact.