mypy-boto3-bedrock Type Stubs

1.42.83 · active · verified Sat Apr 11

mypy-boto3-bedrock provides comprehensive type annotations for the `boto3` Bedrock service. It is part of the `boto3-stubs` ecosystem, automatically generated by `mypy-boto3-builder`, ensuring compatibility with `boto3`'s official releases. This library enhances static analysis with tools like `mypy` and `pyright`, offering improved code completion and helping identify potential bugs in `boto3` Bedrock client usage. The version number of `mypy-boto3-bedrock` is synchronized with the corresponding `boto3` version it provides stubs for.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up type checking for the `boto3` Bedrock client using `mypy-boto3-bedrock`. It explicitly type hints the `boto3` client with `BedrockClient` and shows how to use a generated `TypeDef` for a response object, enabling static analysis and IDE autocompletion for Bedrock API calls.

import os
from typing import TYPE_CHECKING

import boto3
from boto3.session import Session

# Conditionally import type stubs only during type checking
# This avoids a runtime dependency on mypy-boto3-bedrock
if TYPE_CHECKING:
    from mypy_boto3_bedrock.client import BedrockClient
    from mypy_boto3_bedrock.type_defs import ListFoundationModelsResponseTypeDef

def get_bedrock_client() -> 'BedrockClient':
    # Ensure AWS credentials are configured (e.g., via environment variables or AWS CLI config)
    # For this example, we use a default session.
    # In a real application, consider explicit credential management.
    session = Session(region_name=os.environ.get('AWS_REGION', 'us-east-1'))
    client: BedrockClient = session.client('bedrock')
    return client

def list_bedrock_foundation_models() -> None:
    client = get_bedrock_client()
    print(f"Client type: {type(client)}")
    try:
        response: ListFoundationModelsResponseTypeDef = client.list_foundation_models()
        print("Successfully listed Bedrock foundation models:")
        for model in response['modelSummaries']:
            print(f"  - {model['modelId']} ({model['modelName']})")
    except Exception as e:
        print(f"Error listing models: {e}")

if __name__ == '__main__':
    list_bedrock_foundation_models()

view raw JSON →