Type annotations for AWS BedrockAgentCore (boto3)

1.42.87 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a type-hinted BedrockAgentCore client using `boto3` and perform a basic `list_agents` operation. It includes explicit type annotations for the client and response, which static analysis tools will leverage for improved code completion and error checking. The `TYPE_CHECKING` block prevents the type stub package from being a runtime dependency.

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

view raw JSON →