# openinference-instrumentation-bedrock

**Python · Instrumentation**

OpenInference Bedrock Instrumentation

- **Module**: `openinference.instrumentation.bedrock`
- **Version**: 0.1.68
- **Python**: >=3.10, <3.15
- **Instruments**: `boto3 >= 1.40.46`

[PyPI](https://pypi.org/project/openinference-instrumentation-bedrock/) · [README](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/README.md) · [Source](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-bedrock)

## Install

```sh
pip install openinference-instrumentation-bedrock
```

## Usage

Instrument once, at startup, before the library is used. The [`BedrockInstrumentor`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.BedrockInstrumentor) reference below lists the options.

```python
from opentelemetry.sdk.trace import TracerProvider
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.bedrock import BedrockInstrumentor

tracer_provider = TracerProvider()
# ...add a span processor that exports to Phoenix or any OTLP backend.

BedrockInstrumentor().instrument(
    tracer_provider=tracer_provider,
    config=TraceConfig(hide_inputs=True),  # optional
)
```

## `openinference.instrumentation.bedrock`

OpenInference instrumentation for AWS Bedrock (boto3 and aioboto3).

This module patches botocore’s ClientCreator and aiobotocore’s AioClientCreator so that Bedrock clients (bedrock-runtime and bedrock-agent-runtime) are instrumented with OpenTelemetry spans. Sync and async clients are handled separately: sync uses \_model_invocation_wrapper and BufferedStreamingBody for invoke_model; async uses \_async_model_invocation_wrapper and \_LazyAsyncInvokeModelBody so that the response body is read only when the caller awaits read(), and span attributes are set at that time.

Streaming APIs (invoke_model_with_response_stream, converse_stream, invoke_agent, etc.) use wrappers that start a span and wrap the event stream so the span is ended when the stream is fully consumed, closed, or dropped.

Request/response body types (from botocore/aiobotocore service shapes): InvokeModel request body is the API `body` payload (shape blob), so kwargs\[“body”] may be str, bytes, or a seekable file-like object (e.g. io.BytesIO); file-like bodies are read and restored before parsing. Response body is botocore’s StreamingBody (sync read) or aiobotocore’s StreamingBody (async read) respectively.

Edge cases and limitations:

- Suppression: All wrappers check \_SUPPRESS_INSTRUMENTATION_KEY first and call through unwrapped without creating spans.
- Errors: API or parse failures set span status to ERROR, record_exception when we handle the exception, and end the span. Sync invoke_model uses a context manager that also records and sets status on any uncaught exception.
- Async invoke_model: Span is completed on first read() of the response body, or in **del** if the body is never read (best-effort).
- Streaming: Span ends when the event stream is fully consumed or on first exception. A stream that is closed or dropped before its end ends the span with the output read so far and leaves the status unset.
- Double consumption of the same stream is not supported; the underlying stream is single-read.
- Missing response body: If InvokeModel returns without a body we raise ValueError after setting span status to ERROR.

### `InstrumentedClient`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L119-L150)

```python
class InstrumentedClient(BaseClient)
```

Type stub for an instrumented Bedrock client.

After \_instrument_client() runs, the client’s Bedrock API methods are replaced with wrapped versions that create spans and set OpenInference attributes. The original methods are stored as _unwrapped_\* so the wrappers can call through.

**Attributes**

| Name | Type and value |
| - | - |
| [`invoke_model`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.invoke_model) | `Callable[..., Any]` |
| [`converse`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.converse) | `Callable[..., Any]` |
| [`invoke_agent`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.invoke_agent) | `Callable[..., Any]` |
| [`invoke_inline_agent`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.invoke_inline_agent) | `Callable[..., Any]` |
| [`retrieve`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.retrieve) | `Callable[..., Any]` |
| [`retrieve_and_generate`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.retrieve_and_generate) | `Callable[..., Any]` |
| [`retrieve_and_generate_stream`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.retrieve_and_generate_stream) | `Callable[..., Any]` |
| [`apply_guardrail`](https://openinf.dev/python/openinference-instrumentation-bedrock.html.md#openinference.instrumentation.bedrock.InstrumentedClient.apply_guardrail) | `Callable[..., Any]` |

### `BufferedStreamingBody`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L153-L178)

```python
class BufferedStreamingBody(raw_stream: IO[bytes], content_length: int)
```

Sync streaming body that buffers the full response on first read(), so we can parse it for span attributes and then reset() to allow the caller to read again.

Used by the sync invoke_model wrapper: read once for attribution, reset(), then return the same body to the caller. Expects botocore’s StreamingBody interface (\_raw_stream, \_content_length).

#### `read`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L169-L173)

```python
def read(amt: Optional[int] = None) -> bytes
```

#### `reset`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L175-L178)

```python
def reset() -> None
```

Rewind the buffer so the body can be read again by the caller.

### `BedrockInstrumentor`

_class_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L613-L697)

```python
class BedrockInstrumentor(BaseInstrumentor)
```

OpenTelemetry instrumentor for AWS Bedrock (boto3 and aiobotocore).

Patches ClientCreator.create_client (sync) and AioClientCreator.create_client (async) so that newly created clients have their Bedrock API methods wrapped. Sync and async clients are instrumented independently; aiobotocore is optional.

#### `instrument`

_method_

```python
def instrument(
    *,
    tracer_provider: Optional[TracerProvider] = None,
    config: Optional[TraceConfig] = None,
    skip_dep_check: bool = False,
) -> None
```

Start tracing `boto3`: patch it so its calls make OpenInference spans. Calling it again does nothing. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

**Parameters**

| Name | Description |
| - | - |
| `tracer_provider` | The tracer provider the spans are made with. Defaults to the global one, `opentelemetry.trace.get_tracer_provider()`. |
| `config` | What the spans record: hide inputs, outputs or messages, or limit image sizes. Defaults to `TraceConfig()`, which reads the `OPENINFERENCE_*` environment variables. See [`TraceConfig`](https://openinf.dev/python/openinference-instrumentation.html.md#openinference.instrumentation.TraceConfig). |
| `skip_dep_check` | Instrument even if the installed version of `boto3` is outside the supported range. |

#### `uninstrument`

_method_

```python
def uninstrument() -> None
```

Stop tracing: undo the patches `instrument()` made. Inherited from OpenTelemetry’s [`BaseInstrumentor`](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/base/instrumentor.html).

#### `instrumentation_dependencies`

_method_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/__init__.py#L628-L629)

```python
def instrumentation_dependencies() -> Collection[str]
```

## `openinference.instrumentation.bedrock.utils.json_utils`

### `parse_invoke_model_body`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/json_utils.py#L11-L41)

```python
def parse_invoke_model_body(body: Any) -> Any
```

Parse an InvokeModel request body that may be str, bytes, or a file-like object.

boto3 documents `body` as “bytes or seekable file-like object”. A file-like body is read from its current position, where botocore also starts sending. If it was already read to the end, as it is once the request is sent, it is read again from the start. Its position is restored afterwards so the request is unaffected.

### `fix_loose_json_string`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/json_utils.py#L44-L73)

```python
def fix_loose_json_string(s: str) -> list[dict[str, Any]]
```

Converts a loosely formatted JSON string into a list of dictionaries.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `s` | `str` | The loosely formatted JSON string. |

**Returns**

| Type | Description |
| - | - |
| `list[dict[str, Any]]` | list\[dict\[str, Any]]: A list of dictionaries parsed from the string. |

### `sanitize_json_input`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/json_utils.py#L76-L92)

```python
def sanitize_json_input(bad_json_str: str) -> str
```

Cleans a JSON string by escaping invalid backslashes.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `bad_json_str` | `str` | The JSON string with potential invalid backslashes. |

**Returns**

| Type | Description |
| - | - |
| `str` | The sanitized JSON string. |

### `safe_json_loads`

_function_ · [Source](https://github.com/Arize-ai/openinference/blob/main/python/instrumentation/openinference-instrumentation-bedrock/src/openinference/instrumentation/bedrock/utils/json_utils.py#L95-L109)

```python
def safe_json_loads(json_str: str) -> Any
```

Safely loads a JSON string, attempting to sanitize it if initial loading fails.

**Parameters**

| Name | Type | Description |
| - | - | - |
| `json_str` | `str` | The JSON string to load. |

**Returns**

| Type | Description |
| - | - |
| `Any` | The loaded JSON object. |
