mypy-boto3-personalize-runtime type stubs
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
- breaking As of `mypy-boto3-builder` version 8.12.0 (which generated this stub version), support for Python 3.8 has been removed. Users on Python 3.8 should upgrade their Python version to 3.9+ or use an older `mypy-boto3` stub package version.
- breaking The `mypy-boto3-builder` (version 8.9.0 and later) introduced changes to `TypeDef` naming conventions. Some generated type definition names may have been shortened or have their `Extra` postfix moved. For example, `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`. While this specific package is for 'personalize-runtime', such a change in the builder could affect similar patterns in type names across services.
- gotcha When using standalone `mypy-boto3-personalize-runtime` packages (rather than the umbrella `boto3-stubs` with extras), explicit type annotations for `boto3.client()` calls are required for proper type checking and IDE autocompletion. The type checker will not automatically infer the specific client type without the annotation.
- gotcha PyCharm users might experience slow performance or high CPU usage due to how it handles `Literal` overloads within `mypy-boto3` stubs. It is sometimes recommended to use `boto3-stubs-lite` (a more RAM-friendly version) or disable PyCharm's internal type checker and rely on `mypy` or `pyright` instead.
- gotcha Pylint might raise `undefined-variable` warnings when using `TYPE_CHECKING` guards to conditionally import `mypy-boto3` types. This is a known issue with Pylint's interaction with the `TYPE_CHECKING` block.
Install
-
pip install mypy-boto3-personalize-runtime boto3
Imports
- PersonalizeRuntimeClient
from mypy_boto3_personalize_runtime.client import PersonalizeRuntimeClient
- GetRecommendationsResponseTypeDef
from mypy_boto3_personalize_runtime.type_defs import GetRecommendationsResponseTypeDef
- boto3
import boto3
Quickstart
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}")