mypy-boto3-personalize-runtime type stubs

1.42.3 · active · verified Sat Apr 11

mypy-boto3-personalize-runtime provides high-quality type annotations for the AWS SDK for Python (boto3)'s PersonalizeRuntime service. It enables static type checking with tools like Mypy, Pyright, and enhances IDE features like autocompletion for boto3 clients, responses, and related data structures. The current version, 1.42.3, is generated with mypy-boto3-builder 8.12.0 and is updated frequently to align with boto3 releases and new AWS service features.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a type-hinted `PersonalizeRuntimeClient` using `boto3` and explicitly annotate the client object and the response from `get_recommendations`. This allows static type checkers to validate your usage of the client and its response, catching potential errors before runtime. Replace placeholder values with your actual AWS campaign ARN and user ID.

import boto3
from typing import TYPE_CHECKING
import os

if TYPE_CHECKING:
    from mypy_boto3_personalize_runtime.client import PersonalizeRuntimeClient
    from mypy_boto3_personalize_runtime.type_defs import GetRecommendationsResponseTypeDef

def get_personalize_recommendations(
    campaign_arn: str, user_id: str, num_results: int = 10
) -> GetRecommendationsResponseTypeDef:
    """Gets recommendations from AWS Personalize Runtime."""
    client: PersonalizeRuntimeClient = boto3.client(
        "personalize-runtime",
        region_name=os.environ.get('AWS_REGION', 'us-east-1'),
        aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', ''),
        aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', '')
    )
    
    response: GetRecommendationsResponseTypeDef = client.get_recommendations(
        campaignArn=campaign_arn,
        userId=user_id,
        numResults=num_results
    )
    return response

# Example usage (requires AWS credentials and a configured Personalize campaign)
# if __name__ == "__main__":
#     campaign_arn_example = "arn:aws:personalize:us-east-1:123456789012:campaign/your-campaign-name"
#     user_id_example = "test_user_1"
#     try:
#         recommendations = get_personalize_recommendations(campaign_arn_example, user_id_example)
#         print("Recommended Items:")
#         for item in recommendations.get("itemList", []):
#             print(f"  Item ID: {item.get('itemId')}")
#     except Exception as e:
#         print(f"Error getting recommendations: {e}")

view raw JSON →