mypy-boto3-bedrock-runtime Type Annotations
mypy-boto3-bedrock-runtime provides comprehensive type annotations for the `boto3` Bedrock Runtime service, version 1.42.82. Generated by `mypy-boto3-builder` (version 8.12.0), it enhances developer experience by offering static type checking, auto-completion, and error detection in IDEs like VSCode and PyCharm, and with type checkers like Mypy and Pyright. The library is actively maintained and frequently updated to synchronize with `boto3` releases and schema changes.
Warnings
- breaking Starting with `mypy-boto3-builder` version 8.12.0, support for Python 3.8 has been removed. All generated packages, including `mypy-boto3-bedrock-runtime`, now require Python 3.9 or newer.
- breaking Version 8.9.0 of `mypy-boto3-builder` introduced breaking changes in TypeDef naming conventions. Specifically, `RequestRequestTypeDef` suffixes were shortened to `RequestTypeDef`, and `Extra` postfixes were moved to the end of TypeDef names (e.g., `CreateDistributionExtraRequestTypeDef` became `CreateDistributionRequestExtraTypeDef`). While `mypy-boto3-bedrock-runtime` specific changes might be rare, these builder-wide changes affect how you reference TypeDefs across all `mypy-boto3` stubs.
- gotcha It is best practice to wrap type stub imports within an `if TYPE_CHECKING:` block. This ensures that the type stubs are only loaded during static analysis by tools like Mypy and Pyright, preventing them from becoming runtime dependencies in your production environment.
- gotcha The `mypy-boto3-bedrock-runtime` package is generated for a specific `boto3` version (1.42.82 in this case). Mismatching `boto3` runtime versions with the installed stub version can lead to incorrect type hints or unresolved attributes, as the API might have changed between `boto3` versions.
- gotcha While `mypy` can often infer types, explicitly annotating the return type of `boto3.client("bedrock-runtime")` with `BedrockRuntimeClient` (and similarly for paginators and waiters) provides the most robust type checking and significantly improves auto-completion in IDEs like VSCode and PyCharm, especially for `boto3`'s dynamically generated clients.
Install
-
pip install mypy-boto3-bedrock-runtime boto3
Imports
- BedrockRuntimeClient
from mypy_boto3_bedrock_runtime import BedrockRuntimeClient
- ListAsyncInvokesPaginator
from mypy_boto3_bedrock_runtime.paginator import ListAsyncInvokesPaginator
- AsyncInvokeStatusType
from mypy_boto3_bedrock_runtime.literals import AsyncInvokeStatusType
- AppliedGuardrailDetailsTypeDef
from mypy_boto3_bedrock_runtime.type_defs import AppliedGuardrailDetailsTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
import os
if TYPE_CHECKING:
from mypy_boto3_bedrock_runtime import BedrockRuntimeClient
from mypy_boto3_bedrock_runtime.type_defs import InvokeModelResponseTypeDef
from mypy_boto3_bedrock_runtime.paginator import ListAsyncInvokesPaginator
def get_bedrock_runtime_client() -> "BedrockRuntimeClient":
"""Returns a typed BedrockRuntime client."""
return boto3.client(
"bedrock-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', ''),
aws_session_token=os.environ.get('AWS_SESSION_TOKEN', '')
)
def invoke_model_example():
client: BedrockRuntimeClient = get_bedrock_runtime_client()
# Example: Invoke a model (replace with actual model_id and body)
response: InvokeModelResponseTypeDef = client.invoke_model(
modelId="anthropic.claude-v2",
contentType="application/json",
accept="application/json",
body='{"prompt": "Human: Hello, how are you? Assistant:", "max_tokens_to_sample": 200}'
)
print(f"Model invocation response: {response['body'].read().decode('utf-8')}")
def list_async_invokes_paginator_example():
client: BedrockRuntimeClient = get_bedrock_runtime_client()
paginator: ListAsyncInvokesPaginator = client.get_paginator("list_async_invokes")
for page in paginator.paginate():
print(f"Async invokes page: {page.get('asyncInvokes')}")
if __name__ == "__main__":
invoke_model_example()
list_async_invokes_paginator_example()