sagemaker-python-sdk icon indicating copy to clipboard operation
sagemaker-python-sdk copied to clipboard

`ModelTrainer` emits invalid lowercase tag keys in pipeline definitions

Open Alex-Mesmer opened this issue 1 month ago • 0 comments

PySDK Version

  • [ ] PySDK V2 (2.x)
  • [x] PySDK V3 (3.x)

Describe the bug

ModelTrainer accepts sagemaker.core.shapes.Tag objects. Interactive training serializes these tags correctly.

When the trainer uses a PipelineSession, _create_training_job_args() calls model_dump() on each tag. This produces dictionaries with lowercase key and value fields.

The pipeline serializer converts only the top-level request keys to Pascal case. The nested tag fields remain lowercase. SageMaker rejects the pipeline definition because the service requires Key and Value.

To reproduce

Set these environment variables:

export AWS_PROFILE="<aws-profile>"
export AWS_REGION="us-east-2"
export SAGEMAKER_ROLE_ARN="arn:aws:iam::<account-id>:role/<sagemaker-execution-role>"
export SAGEMAKER_BUCKET="<existing-bucket>"

Run this script:

import json
import os

import boto3
from sagemaker.core.shapes import Tag
from sagemaker.core.training.configs import Compute, InputData
from sagemaker.core.workflow.pipeline_context import PipelineSession
from sagemaker.mlops.workflow.pipeline import Pipeline
from sagemaker.mlops.workflow.steps import TrainingStep
from sagemaker.train import ModelTrainer

region = os.environ["AWS_REGION"]
role_arn = os.environ["SAGEMAKER_ROLE_ARN"]
bucket = os.environ["SAGEMAKER_BUCKET"]
boto_session = boto3.Session(
    profile_name=os.environ["AWS_PROFILE"],
    region_name=region,
)
pipeline_session = PipelineSession(
    boto_session=boto_session,
    default_bucket=bucket,
)

trainer = ModelTrainer(
    training_image=(
        "257758044811.dkr.ecr.us-east-2.amazonaws.com/"
        "sagemaker-scikit-learn:1.4-2-py312-cpu-py3"
    ),
    role=role_arn,
    sagemaker_session=pipeline_session,
    compute=Compute(
        instance_type="ml.m6i.large",
        instance_count=1,
        volume_size_in_gb=30,
    ),
    base_job_name="modeltrainer-tag-reproduction",
    tags=[
        Tag(
            key="project",
            value="pipeline-tag-reproduction",
        )
    ],
)

step_args = trainer.train(
    input_data_config=[
        InputData(
            channel_name="train",
            data_source=f"s3://{bucket}/train/",
        )
    ]
)
training_step = TrainingStep(
    name="modeltrainer-tag-reproduction",
    step_args=step_args,
)
pipeline = Pipeline(
    name="modeltrainer-tag-reproduction",
    steps=[training_step],
    sagemaker_session=pipeline_session,
)

definition = json.loads(pipeline.definition())
print(definition["Steps"][0]["Arguments"]["Tags"])

pipeline.upsert(role_arn=role_arn)

The printed pipeline definition contains:

[
  {
    "key": "project",
    "value": "pipeline-tag-reproduction"
  }
]

The CreatePipeline request fails.

Expected behavior

The pipeline definition must contain boto-style tag keys:

[
  {
    "Key": "project",
    "Value": "pipeline-tag-reproduction"
  }
]

The core serializer already handles v3 Tag models correctly:

from sagemaker.core.shapes import Tag
from sagemaker.core.utils.utils import serialize

serialize(Tag(key="project", value="example"))
# {"Key": "project", "Value": "example"}

A possible fix is to keep the validated Tag objects in the training request:

-        tags_as_dicts = None
-        if self.tags:
-            tags_as_dicts = []
-            for tag in self.tags:
-                if hasattr(tag, "model_dump"):
-                    tags_as_dicts.append(tag.model_dump())
-                elif isinstance(tag, dict):
-                    tags_as_dicts.append(tag)
-                else:
-                    tags_as_dicts.append(
-                        {"key": getattr(tag, "key", ""), "value": getattr(tag, "value", "")}
-                    )
-
         training_request = {
             ...
-            "tags": tags_as_dicts,
+            "tags": self.tags,
             ...
         }

Add tests for both paths:

  • Interactive TrainingJob.create() receives valid tags.
  • Pipeline.definition() contains Key and Value.

Screenshots or logs

botocore.exceptions.ClientError: An error occurred (ValidationException)
when calling the CreatePipeline operation:
Unable to parse pipeline definition. Unknown Argument member 'key'.

System information

  • SageMaker Python SDK version: 3.24.0
  • Framework name (eg. PyTorch) or algorithm (eg. KMeans): SKLearn managed image
  • Framework version: 1.4-2-py312
  • Python version: 3.12.0
  • CPU or GPU: CPU, Apple Silicon (arm64)
  • Custom Docker image (Y/N): N

Additional context

This affects ModelTrainer only when it creates pipeline step arguments. Interactive training with the same Tag objects works.

The same conversion logic is present in the SageMaker Python SDK v3.25.0 tagged source.

Alex-Mesmer avatar Oct 14 '26 20:10 Alex-Mesmer