{"library":"openinference-instrumentation-bedrock","title":"OpenInference Bedrock Instrumentation","description":"The `openinference-instrumentation-bedrock` library provides automatic instrumentation for the AWS Bedrock client (`boto3`), enabling OpenTelemetry-compliant observability for applications utilizing Bedrock foundation models. It captures detailed traces of LLM invocations and interactions, which can be sent to any OpenTelemetry-compatible backend, such as Arize AI's Phoenix platform. The library is actively maintained by Arize AI, with frequent updates across its various instrumentation packages, as evidenced by its rapid minor version releases. [1, 3, 4, 11, 17]","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install openinference-instrumentation-bedrock boto3"],"cli":null},"imports":["from openinference.instrumentation.bedrock import BedrockInstrumentor"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nimport boto3\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter\nfrom openinference.instrumentation.bedrock import BedrockInstrumentor\n\n# 1. Configure OpenTelemetry Tracer Provider\nresource = Resource.create({\"service.name\": \"my-bedrock-app\"})\ntracer_provider = TracerProvider(resource=resource)\ntracer_provider.add_span_processor(\n    SimpleSpanProcessor(ConsoleSpanExporter())\n)\ntrace.set_tracer_provider(tracer_provider)\n\n# 2. Instrument the Bedrock client\nBedrockInstrumentor().instrument()\n\n# 3. Create a boto3 client (must be after instrumentation)\n# Ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION_NAME are set in environment\n# or configure boto3 credentials separately.\nbedrock_runtime = boto3.client(\n    service_name='bedrock-runtime',\n    region_name=os.environ.get('AWS_REGION_NAME', 'us-east-1'),\n    aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'YOUR_AWS_ACCESS_KEY_ID'),\n    aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'YOUR_AWS_SECRET_ACCESS_KEY')\n)\n\n# 4. Invoke a Bedrock model\nmodel_id = \"anthropic.claude-instant-v1\"\ncontent_type = \"application/json\"\naccept_type = \"application/json\"\n\nbody = {\n    \"prompt\": \"Human: What is the capital of France? Assistant:\",\n    \"max_tokens_to_sample\": 100,\n    \"temperature\": 0.5,\n}\n\ntry:\n    response = bedrock_runtime.invoke_model(\n        body=str(body),\n        modelId=model_id,\n        contentType=content_type,\n        accept=accept_type\n    )\n    response_body = response['body'].read().decode('utf-8')\n    print(f\"Model Response: {response_body}\")\nexcept Exception as e:\n    print(f\"Error invoking model: {e}\")\n    print(\"Please ensure your AWS credentials are configured and Bedrock access is granted.\")\n\n# Spans will be printed to console by ConsoleSpanExporter","lang":"python","description":"This quickstart demonstrates how to set up `openinference-instrumentation-bedrock` to automatically trace calls to AWS Bedrock. It configures a basic OpenTelemetry `TracerProvider` with a `ConsoleSpanExporter` to print traces to the console, instruments the `boto3` Bedrock client, and then makes a sample `invoke_model` call. Ensure your AWS credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION_NAME) are set as environment variables or configured in your AWS setup for `boto3` to work correctly. [1, 3, 4, 13]","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}