mypy-boto3-sagemaker-runtime Type Stubs

1.42.54 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

Demonstrates how to obtain a type-hinted SageMaker Runtime client and use it to invoke an endpoint. It highlights the use of `SagemakerRuntimeClient` for the client and `InvokeEndpointOutputTypeDef` for the operation's response, ensuring type safety throughout the interaction.

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.")

view raw JSON →