mypy-boto3-bedrock-agent

1.42.83 · active · verified Sat Apr 11

mypy-boto3-bedrock-agent provides type annotations (stubs) for the `boto3` Bedrock Agent service, enhancing static type checking with tools like Mypy. It is part of the `mypy-boto3` ecosystem, which generates stubs for all `boto3` services. The library follows the `boto3` release cycle closely, with frequent updates to ensure compatibility with the latest AWS SDK features and API changes. Current version is 1.42.83.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `bedrock-agent` client with type hints from `mypy-boto3-bedrock-agent` and perform a basic operation (`list_agents`). It highlights how the stubs provide static type checking for `boto3` client methods and their responses.

import os
import boto3
from mypy_boto3_bedrock_agent import BedrockAgentClient
from botocore.exceptions import ClientError

# Ensure boto3 is installed and configured (e.g., via AWS environment variables
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, or IAM roles).
# For local testing, ensure these are set or provide dummy values.

# Type hint the client to enable static analysis by Mypy
# region_name is typically required for Bedrock services.
client: BedrockAgentClient = boto3.client(
    "bedrock-agent",
    region_name=os.environ.get("AWS_REGION", "us-east-1")
)

try:
    # Example operation: List existing agents
    print("Attempting to list Bedrock Agents...")
    response = client.list_agents(maxResults=5)
    
    agents = response.get("agentSummaries", [])
    if agents:
        print("Successfully listed agents:")
        for agent_summary in agents:
            print(f"  - Agent ID: {agent_summary['agentId']}, Name: {agent_summary['agentName']}")
    else:
        print("No Bedrock agents found in this region.")

except ClientError as e:
    error_code = e.response.get("Error", {}).get("Code")
    error_message = e.response.get("Error", {}).get("Message")
    if error_code == "AccessDeniedException":
        print(f"Access Denied: You might not have permissions to list agents. Error: {error_message}")
    else:
        print(f"An AWS Client Error occurred: {error_code} - {error_message}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

# Mypy will now correctly type-check method calls on `client`
# and the structure of `response` based on the AWS API definition.

view raw JSON →