mypy-boto3-bedrock Type Stubs
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
- breaking Starting with `mypy-boto3-builder` version 8.12.0 (which generated `mypy-boto3-bedrock` 1.42.83 and newer), support for Python 3.8 has been removed. Projects using these stubs must target Python 3.9 or newer.
- breaking All `mypy-boto3-*` packages, including `mypy-boto3-bedrock`, have migrated to PEP 561 compliant distribution. This changes how type checkers might discover and consume the stubs.
- breaking Service-specific `TypeDef` naming conventions changed for clarity in `mypy-boto3-builder` 8.9.0. For example, `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`. This affects direct imports of generated `TypeDef` names.
- gotcha PyCharm users may experience performance issues with `Literal` overloads when using full `boto3-stubs` packages. The `boto3-stubs-lite` variants are recommended as a workaround for PyCharm until this issue is resolved.
- gotcha Pylint might report 'undefined variable' errors when using type stubs without `TYPE_CHECKING` guards, as it attempts to evaluate imports at runtime.
Install
-
pip install mypy-boto3-bedrock boto3 -
pip install 'boto3-stubs[bedrock]' boto3
Imports
- BedrockClient
from mypy_boto3_bedrock.client import BedrockClient
- AgreementStatusType
from mypy_boto3_bedrock.literals import AgreementStatusType
- ModelEnforcementOutputTypeDef
from mypy_boto3_bedrock.type_defs import ModelEnforcementOutputTypeDef
Quickstart
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()