Skip to content

fix(train): lazily create AIRHub boto3 clients to avoid NoRegionError on import - #6401

Open
AkshayShah03 wants to merge 1 commit into
aws:masterfrom
AkshayShah03:fix/airhub-lazy-boto3-clients
Open

AkshayShah03 wants to merge 1 commit into
aws:masterfrom
AkshayShah03:fix/airhub-lazy-boto3-clients

Conversation

@AkshayShah03

Copy link
Copy Markdown

Issue #, if available: N/A (same problem as closed, unmerged #5935)

Description of changes:

AIRHub created its default boto3.client("sagemaker") and boto3.client("s3") as class attributes, so they were built at import time. model_builder.py imports MultiTurnRLTrainer, which imports AIRHub, so import sagemaker.serve raises NoRegionError on any machine or CI job without an AWS region configured:

$ env -u AWS_DEFAULT_REGION -u AWS_REGION python -c "import sagemaker.serve"
...
  File ".../sagemaker/ai_registry/air_hub.py", line 36, in AIRHub
    _sagemaker_client = boto3.client("sagemaker")
botocore.exceptions.NoRegionError: You must specify a region.

This change creates the default clients on first use, through _get_sagemaker_client() and _get_s3_client(), and caches them on the class. The _sagemaker_client / _s3_client attributes are kept (default None), so code and tests that assign mock clients to them keep working.

Testing:

  • test_import_does_not_require_aws_region: imports the module in a subprocess with no region or credentials configured. It fails before this change and passes after.
  • test_clients_created_lazily_and_cached: each client is created once, on first use.
  • sagemaker-train/tests/unit/ai_registry: 98 passed (96 before, plus the 2 new tests).
  • import sagemaker.core, sagemaker.train, sagemaker.serve, sagemaker.mlops now succeeds with no region configured.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

… on import

AIRHub created boto3 sagemaker/s3 clients as class attributes, so they were
constructed at import time. Since model_builder imports MultiTurnRLTrainer,
'import sagemaker.serve' transitively imports AIRHub and raises NoRegionError
in any environment without an AWS region configured.

Create the default clients on first use via _get_sagemaker_client() and
_get_s3_client() and cache them on the class. The attributes are kept so
existing callers/tests that assign mock clients keep working.

This branch is waiting to be deployed

1 waiting deployment
manual-approval — 7672ee39 Waiting Oct 9, 2026 by AkshayShah03 via wait-for-approval #580
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant