Skip to content

Commit ab7524e

Browse files
fix: don't treat SageMaker model-package ARN as a HuggingFace model ID (#6513)
ModelBuilder's smart-detection path for choosing a default instance type treated any non-JumpStart string `model` value as a HuggingFace repo id. A SageMaker model-package ARN (a supported `model=` input) was pasted verbatim into https://huggingface.co/api/models/<ARN>, which 404s. The 404 handler only recognized HTTP 401, so it fell through and the failure surfaced as a misleading "provide a HUGGING_FACE_HUB_TOKEN" message. Changes: - Guard the smart-detection branch in `_get_default_instance_type` so a model-package ARN skips HuggingFace metadata lookup and falls back to the CPU default instead of hitting huggingface.co. - Exclude model-package ARNs in `_is_huggingface_model` (an ARN contains "/", which the org/model-name heuristic wrongly matched). - Distinguish HTTP 404 from 401 in `get_huggingface_model_metadata` so a missing repo reports "not found on huggingface.co" instead of suggesting a HUGGING_FACE_HUB_TOKEN. Reuses the existing MODEL_PACKAGE_ARN_REGEX helper. Adds regression tests. Co-authored-by: Mufaddal Rohawala <mufi@amazon.com>
1 parent c9ec795 commit ab7524e

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎sagemaker-serve/src/sagemaker/serve/model_builder_utils.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ def _get_default_instance_type(self) -> str:
356356
logger.debug(f"Using JumpStart recommended instance type: {recommended_type}")
357357
return recommended_type
358358

359+
# A SageMaker model-package ARN is not a HuggingFace repo id. Routing it
360+
# to the HF metadata API 404s and produces a misleading "provide a
361+
# HUGGING_FACE_HUB_TOKEN" error, so skip smart detection for ARNs.
362+
elif isinstance(self.model, str) and re.match(MODEL_PACKAGE_ARN_REGEX, self.model):
363+
logger.debug(
364+
"Model is a SageMaker model-package ARN, not a HuggingFace model ID; "
365+
"skipping HuggingFace-based instance type detection."
366+
)
367+
359368
# For HuggingFace models, use metadata to detect requirements
360369
elif isinstance(self.model, str):
361370
try:
@@ -532,6 +541,11 @@ def _is_huggingface_model(self) -> bool:
532541
if not isinstance(self.model, str):
533542
return False
534543

544+
# A SageMaker model-package ARN contains "/" but is not a HuggingFace
545+
# model ID; exclude it before the organization/model-name heuristic.
546+
if re.match(MODEL_PACKAGE_ARN_REGEX, self.model):
547+
return False
548+
535549
# Simple pattern matching for HuggingFace model IDs
536550
# Format: "organization/model-name" or just "model-name"
537551
model_type = getattr(self, "model_type", None)
@@ -3456,6 +3470,13 @@ def get_huggingface_model_metadata(
34563470
"Trying to access a gated/private HuggingFace model without valid credentials. "
34573471
"Please provide a HUGGING_FACE_HUB_TOKEN in env_vars"
34583472
)
3473+
if "HTTP Error 404: Not Found" in str(e):
3474+
raise ValueError(
3475+
"HuggingFace model ID %s was not found on huggingface.co. Please verify the "
3476+
"model ID is a valid HuggingFace repo. If you intended to deploy a SageMaker "
3477+
"model-package or other non-HuggingFace artifact, it should not be passed as a "
3478+
"HuggingFace model ID." % model_id
3479+
)
34593480
logger.warning(
34603481
"Exception encountered while trying to retrieve HuggingFace model metadata %s. "
34613482
"Details: %s",

‎sagemaker-serve/tests/unit/test_model_builder_utils_final_gaps.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,5 +526,52 @@ def test_get_default_instance_type_fallback(self, mock_is_js):
526526
self.assertEqual(result, "ml.m5.large")
527527

528528

529+
class TestModelPackageArnNotTreatedAsHuggingFace(unittest.TestCase):
530+
"""Regression tests: a SageMaker model-package ARN must not be routed to HF."""
531+
532+
MODEL_PACKAGE_ARN = (
533+
"arn:aws:sagemaker:us-west-2:047646727321:model-package/complaint-clf-qwen3-8b/1"
534+
)
535+
536+
@patch.object(_ModelBuilderUtils, "get_huggingface_model_metadata")
537+
@patch.object(_ModelBuilderUtils, "_is_jumpstart_model_id")
538+
def test_arn_skips_hf_metadata_and_uses_cpu_default(self, mock_is_js, mock_get_metadata):
539+
"""A model-package ARN must not be sent to the HuggingFace metadata API."""
540+
utils = _ModelBuilderUtils()
541+
utils.model = self.MODEL_PACKAGE_ARN
542+
utils.env_vars = {}
543+
544+
mock_is_js.return_value = False
545+
546+
result = utils._get_default_instance_type()
547+
548+
# HF metadata must never be fetched for an ARN.
549+
mock_get_metadata.assert_not_called()
550+
self.assertEqual(result, "ml.m5.large")
551+
552+
def test_arn_is_not_huggingface_model(self):
553+
"""_is_huggingface_model must return False for a model-package ARN."""
554+
utils = _ModelBuilderUtils()
555+
utils.model = self.MODEL_PACKAGE_ARN
556+
utils._is_jumpstart_model_id = Mock(return_value=False)
557+
558+
self.assertFalse(utils._is_huggingface_model())
559+
560+
@patch("urllib.request.urlopen")
561+
def test_hf_metadata_404_does_not_suggest_hf_token(self, mock_urlopen):
562+
"""A 404 from HuggingFace must not emit a gated/private HF token message."""
563+
from urllib.error import HTTPError
564+
565+
utils = _ModelBuilderUtils()
566+
mock_urlopen.side_effect = HTTPError(None, 404, "Not Found", None, None)
567+
568+
with self.assertRaises(ValueError) as context:
569+
utils.get_huggingface_model_metadata("nonexistent-model")
570+
571+
message = str(context.exception)
572+
self.assertIn("was not found on huggingface.co", message)
573+
self.assertNotIn("HUGGING_FACE_HUB_TOKEN", message)
574+
575+
529576
if __name__ == "__main__":
530577
unittest.main()

0 commit comments

Comments
 (0)