Type annotations for AWS BedrockAgentCore (boto3)
mypy-boto3-bedrock-agentcore provides type annotations for the boto3 BedrockAgentCore service, enhancing developer experience with static analysis tools like Mypy, Pyright, and IDEs such as VSCode and PyCharm. It's automatically generated by `mypy-boto3-builder` (currently v8.12.0) and typically releases new versions in sync with `boto3` updates, ensuring up-to-date type information for AWS SDK for Python users. This is an active project with frequent updates.
Warnings
- breaking Python 3.8 support has been removed in `mypy-boto3-builder` version 8.12.0, which generates these type stubs. Projects using `mypy-boto3-bedrock-agentcore` will no longer be type-checked or compatible with Python 3.8 environments.
- breaking Type definition (TypeDef) names for packed method arguments were shortened in `mypy-boto3-builder` version 8.9.0. This change might break existing type hints if you were explicitly importing and using these TypeDefs (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`).
- gotcha For full auto-completion and type checking benefits, especially in IDEs like VSCode or PyCharm, it is highly recommended to explicitly annotate the return type of `boto3.client()` calls with the specific `mypy-boto3` client class.
- gotcha When using `if TYPE_CHECKING:` guards to avoid runtime dependency on `mypy-boto3` packages, Pylint might incorrectly report undefined variables. The `mypy-boto3` documentation provides a workaround.
Install
-
pip install boto3 mypy-boto3-bedrock-agentcore
Imports
- BedrockAgentCoreClient
from mypy_boto3_bedrock_agentcore import BedrockAgentCoreClient
- ListAgentsResponseTypeDef
from mypy_boto3_bedrock_agentcore.type_defs import ListAgentsResponseTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING, List
import os
# These imports are only for type checking, not for runtime execution
if TYPE_CHECKING:
from mypy_boto3_bedrock_agentcore import BedrockAgentCoreClient
from mypy_boto3_bedrock_agentcore.type_defs import AgentSummaryTypeDef, ListAgentsResponseTypeDef
def get_bedrock_agent_core_client() -> "BedrockAgentCoreClient":
"""
Returns a type-hinted BedrockAgentCore client.
"""
# boto3.client returns a botocore.client.BedrockAgentCore instance,
# which mypy-boto3 stubs enhance with static type information.
# Ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION are set as environment variables
client: BedrockAgentCoreClient = boto3.client(
"bedrock-agent-core",
region_name=os.environ.get('AWS_REGION', 'us-east-1') # Default region if not set
)
return client
def list_bedrock_agents():
"""
Lists Bedrock Agents with type hints.
"""
client = get_bedrock_agent_core_client()
print("Listing Bedrock Agents...")
try:
# The response is type-checked against ListAgentsResponseTypeDef
response: ListAgentsResponseTypeDef = client.list_agents()
agents: List[AgentSummaryTypeDef] = response.get("agentSummaries", [])
if agents:
for agent in agents:
print(f" Agent Name: {agent.get('agentName')}, Status: {agent.get('agentStatus')}")
else:
print(" No Bedrock Agents found in the configured region.")
except Exception as e:
print(f"Error listing agents: {e}")
if __name__ == "__main__":
# Example of setting environment variables (replace with your actual credentials/region)
# os.environ['AWS_ACCESS_KEY_ID'] = 'YOUR_ACCESS_KEY'
# os.environ['AWS_SECRET_ACCESS_KEY'] = 'YOUR_SECRET_KEY'
# os.environ['AWS_REGION'] = 'us-east-1'
# This will attempt to use credentials from environment variables, ~/.aws/credentials, or IAM role.
list_bedrock_agents()