Skip to main content

AWS Bedrock Invoke Python

AWS Bedrock Invoke Python is a small library for invoking Claude 4.x models on AWS Bedrock with one consistent request path.

It focuses only on the transport layer:

  • boto3 client creation
  • optional client injection
  • Bedrock model ID / inference-profile resolution
  • Bedrock prompt-cache helpers
  • manual retry with env-driven configuration
  • response text extraction
  • fenced JSON cleanup and parsing

It intentionally does not own business rules such as batching strategies, workflow orchestration, application result caching, or prompt authoring.

Installation

pip install aws-bedrock-invoke

Scope

This library is intentionally narrow:

  • Claude 4.x on AWS Bedrock only
  • manual retry managed by the library
  • Bedrock prompt-cache support
  • text and JSON invoke helpers

This library intentionally does not include:

  • business-specific batching/windowing
  • application result caching
  • Slack notifications or persistence
  • prompt engineering or workflow orchestration

Quick Start

invoke_json()

from aws_bedrock_invoke import BedrockInvoker, json_text_block

invoker = BedrockInvoker.from_env()

result = invoker.invoke_json(
    model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
    profile_arn_fmt="arn:aws:bedrock:{region}:354173731728:inference-profile/{model}",
    system_prompt="Reply with strict JSON.",
    user_content=[
        json_text_block({"question": "hello"}),
    ],
)

print(result.parsed_json)
print(result.response_text)

invoke_text()

from aws_bedrock_invoke import BedrockInvoker, text_block

invoker = BedrockInvoker.from_env()

result = invoker.invoke_text(
    model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
    profile_arn_fmt="arn:aws:bedrock:{region}:354173731728:inference-profile/{model}",
    system_prompt="Answer briefly.",
    user_content=[
        text_block("What is a lien waiver?"),
    ],
)

print(result.response_text)

Client Injection

Client injection is optional.

If you do not provide a client, BedrockInvoker builds one from BedrockClientConfig.

If you already have a prepared boto Bedrock client, you can inject it directly. This is especially useful for adapters and tests.

import boto3

from aws_bedrock_invoke import BedrockClientConfig, BedrockInvoker, text_block

client = boto3.client(
    "bedrock-runtime",
    region_name="us-east-1",
    aws_access_key_id="...",
    aws_secret_access_key="...",
)

invoker = BedrockInvoker(
    client_config=BedrockClientConfig(
        region="us-east-1",
        aws_access_key_id="...",
        aws_secret_access_key="...",
    ),
    client=client,
)

result = invoker.invoke_text(
    model="arn:aws:bedrock:us-east-1:123456789012:inference-profile/example",
    system_prompt="Answer briefly.",
    user_content=[text_block("hello")],
)

Environment Variables

AWS / Bedrock

  • AWS_REGION
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

Retry

  • BEDROCK_RETRY_MAX_ATTEMPTS
  • BEDROCK_RETRY_BASE_SECONDS
  • BEDROCK_RETRY_MAX_SECONDS
  • BEDROCK_RETRY_JITTER
  • BEDROCK_RETRYABLE_ERROR_CODES

The Bedrock SDK retry behavior is effectively disabled in the client, and the library uses its own manual progressive retry loop instead.

Default retryable error codes:

  • ServiceUnavailableException
  • InternalServerException

BEDROCK_RETRYABLE_ERROR_CODES is additive. If you set it, the provided codes are added on top of those defaults rather than replacing them.

Default retry behavior:

  • BEDROCK_RETRY_MAX_ATTEMPTS=4
  • BEDROCK_RETRY_BASE_SECONDS=5
  • BEDROCK_RETRY_MAX_SECONDS=45
  • BEDROCK_RETRY_JITTER=true

on_retry receives a typed RetryEvent with:

  • attempt
  • max_attempts
  • error_code
  • error
  • sleep_seconds
  • model_id
  • elapsed_seconds

Prompt Cache

  • BEDROCK_PROMPT_CACHE_ENABLED
  • BEDROCK_PROMPT_CACHE_TTL

Public API

  • BedrockInvoker
  • BedrockClientConfig
  • RetryPolicy
  • BedrockPromptCacheConfig
  • InvokeResult
  • RetryEvent
  • json_text_block
  • text_block
  • resolve_model_id

Model Resolution

If model is a full ARN, the library uses it directly.

If model is a shorthand like us.anthropic..., you must provide profile_arn_fmt on the invoke call.

Testing

Run the test suite locally with:

PYTHONPATH=src python3 -m unittest discover -s tests -v

Examples

Runnable examples live in examples:

PYTHONPATH=src python3 examples/invoke_json_example.py
PYTHONPATH=src python3 examples/invoke_text_example.py

Consumer Integration

This repo includes library-level tests and Bedrock smoke examples.

Consumer repositories should still add their own integration smoke that uses their real prompt builders and payload-shaping logic through this library.

Metadata

Release files for aws-bedrock-invoke 1.0.0

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

Source distribution (sdist)

Source distribution for aws-bedrock-invoke 1.0.0
File Size Uploaded
aws_bedrock_invoke-1.0.0.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-bedrock-invoke 1.0.0
File Interpreter ABI Platform
aws_bedrock_invoke-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / aws_bedrock_invoke-1.0.0.tar.gz

Download URL aws_bedrock_invoke-1.0.0.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ff5df4f380a61503de1094e8c048861fed33f6f5be5faa992aa6ae5ca875b493
BLAKE2b-256 checksum
How to use checksums
09f4aa087dbb9661559e9ea2844a0e1f6b77a8d69d8084901dc7e7489e1fcd95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / aws_bedrock_invoke-1.0.0-py3-none-any.whl

Download URL aws_bedrock_invoke-1.0.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33582b32e4c05996fe2eb6fbd10e08cc00c53e120cb6be663703d021568efc96
BLAKE2b-256 checksum
How to use checksums
999f82a90b060ec1755abee5a77924d7576b997726979053d31d93c1a71be7c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.0.0 This release

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