121121from sagemaker .deprecations import deprecated_class
122122from sagemaker .enums import EndpointType
123123from sagemaker .inputs import ShuffleConfig , TrainingInput , BatchDataCaptureConfig
124- from sagemaker .user_agent import prepend_user_agent
124+ from sagemaker .user_agent import get_user_agent_extra_suffix
125125from sagemaker .utils import (
126126 name_from_image ,
127127 secondary_training_status_changed ,
@@ -285,6 +285,7 @@ def _initialize(
285285 Creates or uses a boto_session, sagemaker_client and sagemaker_runtime_client.
286286 Sets the region_name.
287287 """
288+
288289 self .boto_session = boto_session or boto3 .DEFAULT_SESSION or boto3 .Session ()
289290
290291 self ._region_name = self .boto_session .region_name
@@ -293,19 +294,30 @@ def _initialize(
293294 "Must setup local AWS configuration with a region supported by SageMaker."
294295 )
295296
296- self .sagemaker_client = sagemaker_client or self .boto_session .client ("sagemaker" )
297- prepend_user_agent (self .sagemaker_client )
297+ # Make use of user_agent_extra field of the botocore_config object
298+ # to append SageMaker Python SDK specific user_agent suffix
299+ # to the current User-Agent header value from boto3
300+ # This config will also make sure that user_agent never fails to log the User-Agent string
301+ # even if boto User-Agent header format is updated in the future
302+ # Ref: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
303+ botocore_config = botocore .config .Config (user_agent_extra = get_user_agent_extra_suffix ())
304+
305+ # Create sagemaker_client with the botocore_config object
306+ # This config is customized to append SageMaker Python SDK specific user_agent suffix
307+ self .sagemaker_client = sagemaker_client or self .boto_session .client (
308+ "sagemaker" , config = botocore_config
309+ )
298310
299311 if sagemaker_runtime_client is not None :
300312 self .sagemaker_runtime_client = sagemaker_runtime_client
301313 else :
302- config = botocore .config .Config (read_timeout = 80 )
314+ config = botocore .config .Config (
315+ read_timeout = 80 , user_agent_extra = get_user_agent_extra_suffix ()
316+ )
303317 self .sagemaker_runtime_client = self .boto_session .client (
304318 "runtime.sagemaker" , config = config
305319 )
306320
307- prepend_user_agent (self .sagemaker_runtime_client )
308-
309321 if sagemaker_featurestore_runtime_client :
310322 self .sagemaker_featurestore_runtime_client = sagemaker_featurestore_runtime_client
311323 else :
@@ -316,8 +328,9 @@ def _initialize(
316328 if sagemaker_metrics_client :
317329 self .sagemaker_metrics_client = sagemaker_metrics_client
318330 else :
319- self .sagemaker_metrics_client = self .boto_session .client ("sagemaker-metrics" )
320- prepend_user_agent (self .sagemaker_metrics_client )
331+ self .sagemaker_metrics_client = self .boto_session .client (
332+ "sagemaker-metrics" , config = botocore_config
333+ )
321334
322335 self .s3_client = self .boto_session .client ("s3" , region_name = self .boto_region_name )
323336 self .s3_resource = self .boto_session .resource ("s3" , region_name = self .boto_region_name )
0 commit comments