Skip to main content

Bedrock Gateway Client

PyPI version Python 3.8+ License: MIT GitHub stars Downloads

A Python client for AWS Bedrock Gateway with IAM authentication. Works with any private API Gateway setup for AWS Bedrock, featuring both Python API and CLI interface.

Features

  • ✅ Configurable - Works with any Bedrock Gateway deployment
  • ✅ No secrets required - Uses your AWS IAM credentials
  • ✅ Simple API - Just client.chat("message")
  • ✅ Multiple configuration methods - Constructor, environment variables, or config file
  • ✅ CLI included - Command-line interface for quick testing

Installation

pip install bedrock-gateway-client==1.0.3

Requirements:

  • Python 3.8+
  • boto3>=1.26.0
  • requests>=2.28.0
  • pyyaml>=6.0
  • click>=8.0.0

Quick Start

Python API

from bedrock_gateway_client import BedrockClient

client = BedrockClient(
    gateway_url="https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke",
    api_id="your-api-id",  # Your API Gateway ID
    region="your-region"
)

response = client.chat("Tell me about koalas")
print(response.text)
print(f"Tokens used: {response.tokens}")

CLI Interface

# Configure once
bedrock-gateway configure --gateway-url "https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke" --api-id "your-api-id" --region your-region

# Chat with Claude
bedrock-gateway chat "Tell me about koalas"

Configuration Options

Direct Configuration

from bedrock_gateway_client import BedrockClient

client = BedrockClient(
    gateway_url="https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke",
    api_id="your-api-id",  # Your API Gateway ID
    region="your-region"
)

Environment Variables

export BEDROCK_GATEWAY_URL="https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke"
export BEDROCK_GATEWAY_API_ID="your-api-id"
export BEDROCK_GATEWAY_REGION="your-region"
from bedrock_gateway_client import BedrockClient

client = BedrockClient()  # Uses environment variables

CLI Configuration

# Save configuration to file
bedrock-gateway configure \
  --gateway-url "https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke" \
  --api-id "your-api-id" \
  --region your-region

Global Configuration

from bedrock_gateway_client import configure, chat

configure(
    gateway_url="https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke",
    api_id="your-api-id",
    region="your-region",
    save=True
)

# Now use simple chat function
response = chat("Tell me about koalas")
print(response)

Usage Examples

Basic Chat

response = client.chat(
    "What is AWS Bedrock?",
    model="sonnet-4.5",
    max_tokens=500
)

print(response.text)
print(f"Tokens: {response.tokens}")
print(f"Latency: {response.latency_ms}ms")

With System Prompt

response = client.chat(
    "What is 2+2?",
    system="You are a helpful math tutor.",
    model="sonnet-4.5"
)

Multi-turn Conversation

# First turn
response1 = client.chat("My name is Alice")

# Build history
history = [
    {"role": "user", "content": [{"text": "My name is Alice"}]},
    {"role": "assistant", "content": [{"text": response1.text}]}
]

# Second turn with context
response2 = client.chat(
    "What's my name?",
    conversation_history=history
)

Command-Line Interface

# Show help
bedrock-gateway --help

# Configure your Bedrock Gateway
bedrock-gateway configure \
  --gateway-url "https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke" \
  --api-id "your-api-id" \
  --region your-region

# Chat with Claude
bedrock-gateway chat "Tell me about koalas"

# Advanced chat with options
bedrock-gateway chat "Explain quantum computing" \
  --model sonnet-4.5 \
  --max-tokens 1000

# Check your AWS identity
bedrock-gateway whoami

Available Models

  • sonnet-4.5 - Best for detailed, complex conversations
  • haiku-4.5 - Fast and efficient for simple tasks

Configuration File

Configuration is saved to ~/.bedrock-gateway/config.yaml:

api_id: your-api-id
gateway_url: https://your-api-gateway.execute-api.region.amazonaws.com/prod/invoke
region: your-region
model_map:
  sonnet-4.5: anthropic.claude-sonnet-4-5-v1:0
  haiku-4.5: anthropic.claude-haiku-4-5-v1:0

Response Object

response.text           # The generated text
response.tokens         # Total tokens used
response.input_tokens   # Input tokens
response.output_tokens  # Output tokens
response.latency_ms     # Response time in milliseconds
response.request_id     # Unique request ID
response.model          # Model used
response.stop_reason    # Why generation stopped
response.raw_response   # Full raw response

Troubleshooting

Common Issues

1. 403 Forbidden Error If you get a 403 Forbidden error, check:

  • Your AWS credentials have the required permissions
  • The API Gateway resource policy allows your role
  • Both gateway_url and api_id are configured correctly
# Verify configuration
bedrock-gateway whoami

# Test with verbose output
bedrock-gateway chat "Hello" --verbose

2. Authentication Errors Ensure your AWS credentials are properly configured:

# Check AWS credentials
aws sts get-caller-identity

# Configure AWS credentials if needed
aws configure

3. Gateway Not Found Make sure both gateway_url and api_id are set correctly in your configuration.

Authentication

Uses your AWS credentials automatically:

  • Default AWS credentials (~/.aws/credentials)
  • AWS profile: BedrockClient(profile="my-profile")
  • Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  • IAM role (when running on EC2/Lambda/ECS)

Required AWS Permissions

Your IAM role needs these permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"
      ],
      "Resource": "arn:aws:execute-api:region:account-id:your-api-id/*/*"
    }
  ]
}

License

MIT License - see LICENSE file for details

Support

For issues, open a GitHub issue at: https://github.com/parv3sh/bedrock-gateway-client/issues

Metadata

Release files for bedrock-gateway-client 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bedrock-gateway-client 1.0.3
File Size Uploaded
bedrock_gateway_client-1.0.3.tar.gz 10.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bedrock-gateway-client 1.0.3
File Interpreter ABI Platform
bedrock_gateway_client-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.5 kB

Release files / bedrock_gateway_client-1.0.3.tar.gz

Download URL bedrock_gateway_client-1.0.3.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c84ce32b23668f7262813addd91c00485879011605c9cd5caf4db09896431141
BLAKE2b-256 checksum
How to use checksums
c1667809894e54747554a0bf23a0c64e6e15eca0a73ff0a77f885db1c95b2ad5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / bedrock_gateway_client-1.0.3-py3-none-any.whl

Download URL bedrock_gateway_client-1.0.3-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3787ba33ce0106ef5fb5b2fa4232070ffeb1c708626db53fd2b15197d14e9389
BLAKE2b-256 checksum
How to use checksums
32482947e50a470366b2aa661e51562c37952aafe5eff1cbce47548f836750bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page