mypy-boto3-bedrock-runtime Type Annotations

1.42.82 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to obtain a type-annotated BedrockRuntime client using `mypy-boto3-bedrock-runtime`. It shows how to call a client method (`invoke_model`) and how to use a paginator (`list_async_invokes`), leveraging `TYPE_CHECKING` for conditional imports. Ensure `boto3` is installed and AWS credentials are configured in your environment or via standard AWS configuration methods.

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()

view raw JSON →