mypy-boto3-sagemaker-runtime Type Stubs
This package provides type annotations for the `boto3` SageMaker Runtime service, generated by `mypy-boto3-builder`. It ensures type safety and autocompletion for `boto3.client('sagemaker-runtime')` operations in your Python projects. The library is actively maintained, with frequent releases that align closely with `boto3` and `botocore` updates.
Warnings
- breaking Python 3.8 support was removed in `mypy-boto3-builder` version 8.12.0 (and consequently for generated stub packages like this one). Projects using Python 3.8 will need to upgrade to Python 3.9+ to use newer versions of these stubs.
- breaking Type definition naming conventions were changed in `mypy-boto3-builder` version 8.9.0. This might affect direct imports of specific `TypeDef` classes if they previously had redundant suffixes (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`).
- gotcha This package provides only type stubs. It does not include the `boto3` runtime library itself. `boto3` must be installed separately for your code to run.
- gotcha Type stubs are generated against specific `boto3` and `botocore` versions. Mismatches between your installed `mypy-boto3-sagemaker-runtime` version and your `boto3`/`botocore` version can lead to incorrect type hints or `mypy` errors, even if the runtime code functions correctly.
Install
-
pip install mypy-boto3-sagemaker-runtime boto3 mypy
Imports
- SagemakerRuntimeClient
from mypy_boto3_sagemaker_runtime.client import SagemakerRuntimeClient
- InvokeEndpointInputRequestTypeDef
from mypy_boto3_sagemaker_runtime.type_defs import InvokeEndpointInputRequestTypeDef
- InvokeEndpointOutputTypeDef
from mypy_boto3_sagemaker_runtime.type_defs import InvokeEndpointOutputTypeDef
Quickstart
import boto3
from mypy_boto3_sagemaker_runtime.client import SagemakerRuntimeClient
from mypy_boto3_sagemaker_runtime.type_defs import InvokeEndpointOutputTypeDef
import os
# Get a type-hinted SageMaker Runtime client
client: SagemakerRuntimeClient = boto3.client("sagemaker-runtime")
# Example: Invoke a SageMaker endpoint
# For a real use case, replace 'your-endpoint-name' with an actual endpoint
# and 'application/json' with the correct ContentType for your model.
endpoint_name = os.environ.get("SAGEMAKER_ENDPOINT_NAME", "your-endpoint-name")
content_type = "application/json"
body = b'{"instances": [[1.0, 2.0, 3.0]]}' # Example JSON payload
try:
# The response object is type-hinted by InvokeEndpointOutputTypeDef
response: InvokeEndpointOutputTypeDef = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType=content_type,
Body=body
)
# The 'Body' key in the response is a StreamingBody object
response_body = response['Body'].read().decode('utf-8')
print(f"Successfully invoked endpoint '{endpoint_name}'.")
print(f"Response Status Code: {response['ResponseMetadata']['HTTPStatusCode']}")
print(f"Response Body: {response_body}")
except client.exceptions.ValidationError as e:
print(f"Validation error: {e}")
print("Please ensure 'SAGEMAKER_ENDPOINT_NAME' environment variable is set to a valid endpoint, and 'Body' matches its expected format.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("This might happen if the endpoint does not exist or credentials are not configured.")