Skip to content

Commit d1b849a

Browse files
committed
fix(jumpstart): keep the Neo accessor read-only and raise at its use site
Return None from the Neo accessor when a launched region has no Neo bucket. Raise in the draft-model resolver when a Neo-backed data source has no bucket. Add an integration test for the JumpStart and Neo bucket boundary. --- X-AI-Prompt: Can you move the missing-Neo-bucket exception from the accessor to the function that uses the Neo bucket? X-AI-Tool: claude-code
1 parent 5ede728 commit d1b849a

3 files changed

Lines changed: 75 additions & 12 deletions

File tree

‎sagemaker-core/src/sagemaker/core/jumpstart/utils.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def get_jumpstart_content_bucket(
220220

221221
def get_neo_content_bucket(
222222
region: str = constants.NEO_DEFAULT_REGION_NAME,
223-
) -> str:
223+
) -> Optional[str]:
224224
"""Returns the regionalized S3 bucket name for Neo service.
225225
226226
Raises:
@@ -236,10 +236,12 @@ def get_neo_content_bucket(
236236
info_log = f"Using Neo bucket override: '{bucket_to_return}'"
237237
constants.JUMPSTART_LOGGER.info(info_log)
238238
else:
239-
region_info = constants.JUMPSTART_REGION_NAME_TO_LAUNCHED_REGION_DICT.get(region)
240-
if region_info is None or region_info.neo_content_bucket is None:
239+
try:
240+
bucket_to_return = constants.JUMPSTART_REGION_NAME_TO_LAUNCHED_REGION_DICT[
241+
region
242+
].neo_content_bucket
243+
except KeyError:
241244
raise ValueError(f"Unable to get content bucket for Neo in {region} region.")
242-
bucket_to_return = region_info.neo_content_bucket
243245

244246
return bucket_to_return
245247

@@ -1699,14 +1701,20 @@ def _add_model_access_configs_to_model_data_sources(
16991701
def get_draft_model_content_bucket(provider: Dict, region: str) -> str:
17001702
"""Returns the correct content bucket for a 1p draft model."""
17011703
if not provider:
1702-
return get_neo_content_bucket(region=region)
1704+
neo_bucket = get_neo_content_bucket(region=region)
1705+
if neo_bucket is None:
1706+
raise ValueError(f"Unable to get content bucket for Neo in {region} region.")
1707+
return neo_bucket
17031708
provider_name = provider.get("name", "")
17041709
if provider_name == "JumpStart":
17051710
classification = provider.get("classification", "ungated")
17061711
if classification == "gated":
17071712
return get_jumpstart_gated_content_bucket(region=region)
17081713
return get_jumpstart_content_bucket(region=region)
1709-
return get_neo_content_bucket(region=region)
1714+
neo_bucket = get_neo_content_bucket(region=region)
1715+
if neo_bucket is None:
1716+
raise ValueError(f"Unable to get content bucket for Neo in {region} region.")
1717+
return neo_bucket
17101718

17111719

17121720
def remove_env_var_from_estimator_kwargs_if_accept_eula_present(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Tests for JumpStart bucket selection."""
14+
from __future__ import absolute_import
15+
16+
import pytest
17+
18+
from sagemaker.core.jumpstart import utils
19+
20+
21+
def test_jumpstart_bucket_resolution_does_not_require_neo():
22+
"""JumpStart uses its bucket when the region has no Neo bucket."""
23+
region = "eusc-de-east-1"
24+
25+
assert utils.get_neo_content_bucket(region) is None
26+
assert (
27+
utils.get_draft_model_content_bucket(
28+
{"name": "JumpStart", "classification": "ungated"}, region
29+
)
30+
== "jumpstart-cache-prod-eusc-de-east-1"
31+
)
32+
33+
with pytest.raises(
34+
ValueError,
35+
match=r"^Unable to get content bucket for Neo in eusc-de-east-1 region\.$",
36+
):
37+
utils.get_draft_model_content_bucket({"name": "Neo"}, region)

‎sagemaker-core/tests/unit/test_jumpstart_utils.py‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,15 @@ def test_get_neo_content_bucket_invalid_region(self):
514514
utils.get_neo_content_bucket("invalid-region")
515515

516516
def test_get_neo_content_bucket_unavailable_in_launched_region(self):
517-
"""A launched region without a Neo content bucket raises an error."""
517+
"""A launched region without a Neo content bucket returns None."""
518518
region_info = Mock()
519519
region_info.neo_content_bucket = None
520520
with patch.object(
521521
constants,
522522
"JUMPSTART_REGION_NAME_TO_LAUNCHED_REGION_DICT",
523523
{"eusc-de-east-1": region_info},
524524
):
525-
with pytest.raises(
526-
ValueError,
527-
match=r"^Unable to get content bucket for Neo in eusc-de-east-1 region\.$",
528-
):
529-
utils.get_neo_content_bucket("eusc-de-east-1")
525+
assert utils.get_neo_content_bucket("eusc-de-east-1") is None
530526

531527

532528
class TestParseSagemakerVersionEdgeCases:
@@ -1909,6 +1905,28 @@ def test_get_draft_model_content_bucket_other_provider(self, mock_neo):
19091905
result = utils.get_draft_model_content_bucket(provider, "us-west-2")
19101906
assert result == "neo-bucket"
19111907

1908+
@patch("sagemaker.core.jumpstart.utils.get_neo_content_bucket", return_value=None)
1909+
def test_get_draft_model_content_bucket_raises_when_neo_is_unavailable(self, mock_neo):
1910+
"""A Neo-backed data source without a Neo bucket raises an error."""
1911+
with pytest.raises(
1912+
ValueError,
1913+
match=r"^Unable to get content bucket for Neo in eusc-de-east-1 region\.$",
1914+
):
1915+
utils.get_draft_model_content_bucket({"name": "Neo"}, "eusc-de-east-1")
1916+
1917+
mock_neo.assert_called_once_with(region="eusc-de-east-1")
1918+
1919+
@patch("sagemaker.core.jumpstart.utils.get_neo_content_bucket", return_value=None)
1920+
def test_get_draft_model_content_bucket_raises_without_provider_or_neo(self, mock_neo):
1921+
"""A missing provider without a Neo bucket raises an error."""
1922+
with pytest.raises(
1923+
ValueError,
1924+
match=r"^Unable to get content bucket for Neo in eusc-de-east-1 region\.$",
1925+
):
1926+
utils.get_draft_model_content_bucket({}, "eusc-de-east-1")
1927+
1928+
mock_neo.assert_called_once_with(region="eusc-de-east-1")
1929+
19121930

19131931
class TestRemoveEnvVarFromEstimatorKwargsIfAcceptEulaPresent:
19141932
"""Test cases for remove_env_var_from_estimator_kwargs_if_accept_eula_present function"""

0 commit comments

Comments
 (0)