Repository navigation
Description
PySDK Version
- PySDK V2 (2.x)
- PySDK V3 (3.x)
Describe the bug
ModelBuilder.register() declares its model_metrics argument as
sagemaker.core.shapes.ModelMetrics.
The registry helper calls model_metrics._to_request_dict(). The declared core
shape does not implement that method.
As a result, the documented type fails with:
AttributeError: 'ModelMetrics' object has no attribute '_to_request_dict'
The older sagemaker.core.model_metrics.ModelMetrics compatibility class has
that method. However, this class differs from the type in the public
ModelBuilder.register() signature.
This defect is present in SageMaker Python SDK v3.25.0.
To reproduce
Set these environment variables:
export AWS_REGION="us-east-2"
export SAGEMAKER_ROLE_ARN="arn:aws:iam::<account-id>:role/<sagemaker-execution-role>"
export IMAGE_URI="<inference-image-uri>"
export MODEL_DATA_URI="s3://<bucket>/model.tar.gz"
export METRICS_URI="s3://<bucket>/evaluation.json"Run this script:
import os
import boto3
from sagemaker.core.shapes import MetricsSource, ModelMetrics, ModelQuality
from sagemaker.core.workflow.pipeline_context import PipelineSession
from sagemaker.serve import ModelBuilder
boto_session = boto3.Session(region_name=os.environ["AWS_REGION"])
pipeline_session = PipelineSession(boto_session=boto_session)
builder = ModelBuilder(
image_uri=os.environ["IMAGE_URI"],
s3_model_data_url=os.environ["MODEL_DATA_URI"],
role_arn=os.environ["SAGEMAKER_ROLE_ARN"],
sagemaker_session=pipeline_session,
instance_type="ml.m5.large",
)
model_metrics = ModelMetrics(
model_quality=ModelQuality(
statistics=MetricsSource(
content_type="application/json",
s3_uri=os.environ["METRICS_URI"],
)
)
)
builder.register(
model_package_group_name="modelbuilder-metrics-reproduction",
content_types=["application/json"],
response_types=["application/json"],
model_metrics=model_metrics,
approval_status="PendingManualApproval",
)No pipeline execution or model package group is required. The failure occurs
while the SDK captures the model-package request.
Expected behavior
ModelBuilder.register() must accept the core ModelMetrics type in its
public signature.
The request must contain this structure:
{
"ModelMetrics": {
"ModelQuality": {
"Statistics": {
"ContentType": "application/json",
"S3Uri": "s3://<bucket>/evaluation.json"
}
}
}
}Screenshots or logs
The failure occurs in sagemaker.core.model_registry.get_model_package_args():
if model_metrics is not None:
model_package_args["model_metrics"] = model_metrics._to_request_dict()System information
- SageMaker Python SDK version: 3.25.0
- Framework name (eg. PyTorch) or algorithm (eg. KMeans): Framework-independent
- Framework version: Not applicable
- Python version: 3.12
- CPU or GPU: CPU
- Custom Docker image (Y/N): Reproduces with either
Additional context
A local workaround subclasses the core shape and adds the missing method:
from sagemaker.core.shapes import ModelMetrics
from sagemaker.core.utils.utils import serialize
class SerializableModelMetrics(ModelMetrics):
def _to_request_dict(self) -> dict:
return serialize(self)A possible SDK fix is to use the core serializer for core shapes:
from sagemaker.core.utils.utils import serialize
model_package_args["model_metrics"] = serialize(model_metrics)The compatibility class must remain supported until its deprecation period is
complete.
Activity
fix: accept core shapes and omitted content types in ModelBuilder.reg…