Type Annotations for boto3 Bedrock Runtime
This package provides comprehensive type annotations (stubs) for the `boto3` Bedrock Runtime client, enabling static type checking with tools like `mypy`. It ensures that your interactions with the AWS Bedrock Runtime service are type-safe, catching potential errors at development time. The current version is 1.42.82, and new releases are frequent, typically aligning with `boto3` and `botocore` updates.
Common errors
-
ModuleNotFoundError: No module named 'types_boto3_bedrock_runtime'
cause Attempting to import the stub package directly at runtime, or the package is not installed.fixEnsure the package is installed (`pip install types-boto3-bedrock-runtime`) and wrap any imports of types with `from typing import TYPE_CHECKING` and `if TYPE_CHECKING:`. -
TypeError: Argument of type 'dict[str, Any]' cannot be assigned to parameter of type 'InvokeModelRequestRequestTypeDef' in call
cause Your dictionary for a request payload does not exactly match the structure defined by the `TypeDef` in the type stubs, or you forgot to pass the required keyword arguments.fixInspect the `InvokeModelRequestRequestTypeDef` (or similar `TypeDef`) definition to ensure your dictionary keys and types match precisely. `mypy` will point out the specific mismatch. -
error: Library "boto3" has no py.typed file (hint: see https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs)
cause You are using `boto3` without its core type stubs (`types-boto3`) or without the correct `mypy` configuration to find these stubs.fixInstall the base `types-boto3` package (`pip install types-boto3`) in addition to `types-boto3-bedrock-runtime`, and ensure `mypy` is configured to include stub paths if necessary (though usually automatic).
Warnings
- breaking Starting with version 8.12.0, `types-boto3-bedrock-runtime` and other `mypy-boto3-builder` packages migrated to PEP 561 standard packages. This means older `mypy` configurations might need adjustment, though most modern setups will handle it seamlessly. Python 3.8 support was also removed.
- breaking In version 8.9.0, there were breaking changes to how TypeDef names are generated, leading to shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`). While this example is not for Bedrock Runtime, it indicates a pattern that might affect specific Bedrock Runtime TypeDefs in future updates if their names become redundant.
- gotcha This package provides only type stubs for static analysis and does not contain any runtime code. You must have the actual `boto3` library installed for your application to run.
- gotcha Directly importing from `types_boto3_bedrock_runtime` at runtime (e.g., `from types_boto3_bedrock_runtime.client import BedrockRuntimeClient`) can lead to `ModuleNotFoundError` or other issues if the stub package is not designed for runtime import, or simply adds unnecessary overhead. The stubs are meant for static checkers like `mypy`.
Install
-
pip install types-boto3-bedrock-runtime boto3 mypy
Imports
- BedrockRuntimeClient
from types_boto3_bedrock_runtime import BedrockRuntimeClient
from typing import TYPE_CHECKING if TYPE_CHECKING: from types_boto3_bedrock_runtime.client import BedrockRuntimeClient - InvokeModelRequestRequestTypeDef
from typing import TYPE_CHECKING if TYPE_CHECKING: from types_boto3_bedrock_runtime.type_defs import InvokeModelRequestRequestTypeDef
Quickstart
import boto3
import os
from typing import TYPE_CHECKING
# Conditional import for type checking
if TYPE_CHECKING:
from types_boto3_bedrock_runtime.client import BedrockRuntimeClient
from types_boto3_bedrock_runtime.type_defs import InvokeModelRequestRequestTypeDef
def invoke_bedrock_model(model_id: str, prompt: str):
# Type annotate the client
client: BedrockRuntimeClient = boto3.client(
'bedrock-runtime',
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET'),
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
body_content = {"prompt": prompt, "max_tokens_to_sample": 200, "temperature": 0.7}
# Example of type-hinted request body (mypy will check its structure)
request_body: InvokeModelRequestRequestTypeDef = {
'body': str(body_content).encode('utf-8'), # In real use, convert dict to JSON string
'contentType': 'application/json',
'accept': 'application/json',
'modelId': model_id
}
try:
response = client.invoke_model(**request_body)
print("Successfully invoked model:")
# Process response body (example)
response_body = response['body'].read().decode('utf-8')
print(response_body)
except client.exceptions.AccessDeniedException as e:
print(f"Access Denied: {e}")
print("Please ensure your AWS credentials and permissions are correctly configured.")
except Exception as e:
print(f"An error occurred: {e}")
# Example usage:
if __name__ == "__main__":
# Replace with an actual model ID available in your region
# e.g., 'anthropic.claude-v2' or 'amazon.titan-text-express-v1'
model = "DUMMY_MODEL_ID"
user_prompt = "Hello, how are you?"
invoke_bedrock_model(model, user_prompt)