Skip to main content

OpenInference AWS Bedrock Instrumentation

Python autoinstrumentation library for AWS Bedrock calls made using boto3 (sync) and aioboto3 (async).

This package implements OpenInference tracing for invoke_model, invoke_agent and converse calls made using the bedrock-runtime and bedrock-agent-runtime clients from both boto3 (sync) and aioboto3 (async).

pypi

[!NOTE]
The Converse API was introduced in botocore v1.34.116. Please use v1.34.116 or above to utilize converse.

Supported Models

Find the list of Bedrock-supported models and their IDs here. Future testing is planned for additional models.

Model Supported Methods
Anthropic Claude 2.0 converse, invoke
Anthropic Claude 2.1 converse, invoke
Anthropic Claude 3 Sonnet 1.0 converse
Anthropic Claude 3.5 Sonnet converse
Anthropic Claude 3 Haiku converse
Meta Llama 3 8b Instruct converse
Amazon Nova Micro converse, invoke
Amazon Nova Lite converse, invoke
Amazon Nova Pro converse, invoke
Meta Llama 3 70b Instruct converse
Mistral AI Mistral 7B Instruct converse
Mistral AI Mixtral 8X7B Instruct converse
Mistral AI Mistral Large converse
Mistral AI Mistral Small converse

Installation

pip install openinference-instrumentation-bedrock

Async (aioboto3) support

To instrument async Bedrock calls made via aioboto3, install aioboto3 in addition to this package:

pip install openinference-instrumentation-bedrock aioboto3

Quickstart

[!IMPORTANT]
OpenInference for AWS Bedrock supports both invoke_model and converse. For models that use the Messages API, such as Anthropic Claude 3 and Anthropic Claude 3.5, use the Converse API instead.

In a notebook environment (jupyter, colab, etc.) install openinference-instrumentation-bedrock, arize-phoenix and boto3.

You can test out this quickstart guide in Google Colab!

pip install openinference-instrumentation-bedrock arize-phoenix boto3

For async usage with aioboto3:

pip install openinference-instrumentation-bedrock arize-phoenix aioboto3

Ensure that boto3 is configured with AWS credentials.

Tracing Setup (Phoenix or Arize AX)

The tracing setup below is shared for both sync (boto3) and async (aioboto3) usage.

from urllib.parse import urljoin

import boto3
import phoenix as px

from openinference.instrumentation.bedrock import BedrockInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

Next, we'll start a phoenix server and set it as a collector.

px.launch_app()
session_url = px.active_session().url
phoenix_otlp_endpoint = urljoin(session_url, "v1/traces")
phoenix_exporter = OTLPSpanExporter(endpoint=phoenix_otlp_endpoint)
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter=phoenix_exporter))
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
BedrockInstrumentor().instrument()

Now, all calls to invoke_model are instrumented and can be viewed in the phoenix UI. When Bedrock returns a model stop reason, the span records it as llm.finish_reason for both invoke_model and converse, including their streaming and async variants.

Quickstart (boto3)

session = boto3.session.Session()
client = session.client("bedrock-runtime")
prompt = b'{"prompt": "Human: Hello there, how are you? Assistant:", "max_tokens_to_sample": 1024}'
response = client.invoke_model(modelId="anthropic.claude-v2", body=prompt)
response_body = json.loads(response.get("body").read())
print(response_body["completion"])

Alternatively, all calls to converse are instrumented and can be viewed in the phoenix UI.

session = boto3.session.Session()
client = session.client("bedrock-runtime")

message1 = {
            "role": "user",
            "content": [{"text": "Create a list of 3 pop songs."}]
}
message2 = {
        "role": "user",
        "content": [{"text": "Make sure the songs are by artists from the United Kingdom."}]
}
messages = []

messages.append(message1)
response = client.converse(
    modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
    messages=messages
)
out = response["output"]["message"]
messages.append(out)
print(out.get("content")[-1].get("text"))

messages.append(message2)
response = client.converse(
    modelId="anthropic.claude-v2:1",
    messages=messages
)
out = response['output']['message']
print(out.get("content")[-1].get("text"))

Amazon Nova models are supported via both invoke_model and converse.

session = boto3.session.Session()
client = session.client("bedrock-runtime")

# invoke_model with Nova
import json
request_body = {
    "schemaVersion": "messages-v1",
    "messages": [{"role": "user", "content": [{"text": "Hello! What is 2+2?"}]}],
    "inferenceConfig": {"maxTokens": 512, "temperature": 0.7},
}
response = client.invoke_model(
    modelId="amazon.nova-micro-v1:0",
    body=json.dumps(request_body),
)
response_body = json.loads(response["body"].read())
print(response_body["output"]["message"]["content"][0]["text"])

All calls to invoke_agent are instrumented and can be viewed in the phoenix UI. You can enable the agent traces by passing enableTrace=True argument.

session = boto3.session.Session()
client = session.client("bedrock-agent-runtime")
agent_id = '<AgentId>'
agent_alias_id = '<AgentAliasId>'
session_id = f"default-session1_{int(time.time())}"

attributes = dict(
    inputText="When is a good time to visit the Taj Mahal?",
    agentId=agent_id,
    agentAliasId=agent_alias_id,
    sessionId=session_id,
    enableTrace=True
)
response = client.invoke_agent(**attributes)

for idx, event in enumerate(response['completion']):
    if 'chunk' in event:
        chunk_data = event['chunk']
        if 'bytes' in chunk_data:
            output_text = chunk_data['bytes'].decode('utf8')
            print(output_text)
    elif 'trace' in event:
        print(event['trace'])

Async Quickstart (aioboto3)

OpenInference AWS Bedrock instrumentation also supports async Bedrock calls using aioboto3.

import aioboto3
import asyncio

async def main():

    session = aioboto3.session.Session(region_name="us-east-1")

    async with session.client(
        "bedrock-runtime",
        aws_access_key_id="test",
        aws_secret_access_key="test",
    ) as client:
        response = await client.converse(
            modelId="anthropic.claude-3-haiku-20240307-v1:0",
            messages=[
                {
                    "role": "user",
                    "content": [{"text": "What is the sum of numbers from 1 to 10?"}],
                }
            ],
        )
        print(response["output"]["message"]["content"][-1]["text"])

asyncio.run(main())

All async calls to invoke_agent are instrumented and can be viewed in the phoenix UI. You can enable the agent traces by passing enableTrace=True argument.

import aioboto3
import asyncio
import time


async def main():

    session = aioboto3.session.Session(region_name="us-east-1")
    agent_id = '<AgentId>'
    agent_alias_id = '<AgentAliasId>'
    session_id = f"default-session1_{int(time.time())}"
    
    attributes = dict(
        inputText="When is a good time to visit the Taj Mahal?",
        agentId=agent_id,
        agentAliasId=agent_alias_id,
        sessionId=session_id,
        enableTrace=True
    )
    async with session.client(
        "bedrock-runtime",
        aws_access_key_id="test",
        aws_secret_access_key="test",
    ) as client:
        response = await client.invoke_agent(**attributes)
        for idx, event in enumerate(response['completion']):
            if 'chunk' in event:
                chunk_data = event['chunk']
                if 'bytes' in chunk_data:
                    output_text = chunk_data['bytes'].decode('utf8')
                    print(output_text)
            elif 'trace' in event:
                print(event['trace'])

asyncio.run(main())

More Info

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openinference_instrumentation_bedrock-0.1.53.tar.gz (242.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file openinference_instrumentation_bedrock-0.1.53.tar.gz.

File metadata

File hashes

Hashes for openinference_instrumentation_bedrock-0.1.53.tar.gz
Algorithm Hash digest
SHA256 9317e46b057f1b0e2ca9497d37b05814719bb786099c01d7e28fdb8c32ca1d40
MD5 c4075d853d84514514ebbf682a0ecd6a
BLAKE2b-256 d3a72b1bd2f61b9bd32f15706875d8047290c074ec73a1d407a93edddb40c908

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_bedrock-0.1.53.tar.gz:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openinference_instrumentation_bedrock-0.1.53-py3-none-any.whl.

File metadata

File hashes

Hashes for openinference_instrumentation_bedrock-0.1.53-py3-none-any.whl
Algorithm Hash digest
SHA256 83d6a4d5ab7ded18d11db04a482e203e1a08935c60024f2b4c9837afe957ac6e
MD5 ca8dda3a6beb16deecfaff29ad834f1b
BLAKE2b-256 ddc921d12084a55fad102c6899dc026ee563fe026365d389c4fbed25d7d7f37a

See more details on using hashes here.

Provenance

The following attestation bundles were made for openinference_instrumentation_bedrock-0.1.53-py3-none-any.whl:

Publisher: publish.yaml on Arize-ai/openinference

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.53 This release

2 files

0.1.52

2 files

0.1.51

2 files

0.1.50

2 files

0.1.49

2 files

0.1.48

2 files

0.1.47

2 files

0.1.46

2 files

0.1.45

2 files

0.1.44

2 files

0.1.43

2 files

0.1.42

2 files

0.1.41

2 files

0.1.40

2 files

0.1.39

2 files

0.1.38

2 files

0.1.37

2 files

0.1.36

2 files

0.1.35

2 files

0.1.34

2 files

0.1.33

2 files

0.1.32

2 files

0.1.31

2 files

0.1.30

2 files

0.1.29

2 files

0.1.28

2 files

0.1.27

2 files

0.1.26

2 files

0.1.25

2 files

0.1.24

2 files

0.1.23

2 files

0.1.22

2 files

0.1.21

2 files

0.1.20

2 files

0.1.19

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 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