mypy-boto3-sagemaker-featurestore-runtime Stubs

1.42.3 · active · verified Sat Apr 11

mypy-boto3-sagemaker-featurestore-runtime provides `mypy` compatible type annotations for `boto3`'s SageMaker Feature Store Runtime service. It helps developers catch type-related errors for `boto3` usage at static analysis time. The library is part of the `mypy-boto3` family, generated by `mypy-boto3-builder`, and its versioning is tied to `boto3`/`botocore` releases, with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-sagemaker-featurestore-runtime` for type-checking a `boto3` client interaction. It defines a function to fetch a record from SageMaker Feature Store, explicitly typing the `boto3` client and the request/response payloads. This allows `mypy` to validate your API calls at static analysis time, catching potential errors before runtime.

import boto3
from mypy_boto3_sagemaker_featurestore_runtime.client import SagemakerFeatureStoreRuntimeClient
from mypy_boto3_sagemaker_featurestore_runtime.type_defs import GetRecordRequestRequestTypeDef, GetRecordResponseTypeDef
import os

def get_feature_store_record(
    feature_group_name: str,
    record_identifier_value_as_string: str
) -> GetRecordResponseTypeDef:
    """Fetches a record from SageMaker Feature Store with type hints."""
    # boto3.client returns an untyped client by default. 
    # The type hint on 'client' variable applies the stubs.
    client: SagemakerFeatureStoreRuntimeClient = boto3.client(
        "sagemaker-featurestore-runtime",
        region_name=os.environ.get("AWS_REGION", "us-east-1")
    )

    request_params: GetRecordRequestRequestTypeDef = {
        "FeatureGroupName": feature_group_name,
        "RecordIdentifierValueAsString": record_identifier_value_as_string,
        "FeatureNames": ["example_feature_name"]
    }

    response: GetRecordResponseTypeDef = client.get_record(**request_params)
    return response

# Example usage (ensure AWS credentials are set up)
if __name__ == "__main__":
    try:
        # Use environment variables for sensitive or dynamic values
        # For a runnable example, provide dummy values if env vars are not set
        example_feature_group_name = os.environ.get("SAGE_FEATURE_GROUP_NAME", "my-test-feature-group")
        example_record_id = os.environ.get("SAGE_RECORD_ID", "12345")

        if not os.environ.get("AWS_ACCESS_KEY_ID") or not os.environ.get("AWS_SECRET_ACCESS_KEY"):
            print("WARNING: AWS credentials not found in environment. Example might fail at runtime.")
            print("Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.")

        print(f"Attempting to get record for Feature Group: {example_feature_group_name}, Record ID: {example_record_id}")
        
        # This call is type-checked by mypy thanks to the stubs
        record_data = get_feature_store_record(example_feature_group_name, example_record_id)
        
        print("Successfully retrieved record (type-checked):")
        if "Record" in record_data:
            for feature in record_data["Record"]:
                print(f"  {feature['FeatureName']}: {feature['ValueAsString']}")
        else:
            print("Record key not found in response, or response is empty.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure AWS credentials are configured and the feature group/record exist/are accessible.")

view raw JSON →