You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix Bedrock instrumentation TextDecoder error with robust type handling (#235)
*Issue #, if available:*
*Description of changes:*
OTel Instrumentation throws TextDecoder error on
`BedrockRuntime.InvokeModelWithResponseStream` API response
- Fix "The 'list' argument must be an instance of SharedArrayBuffer,
ArrayBuffer or ArrayBufferView" error in
BedrockRuntimeServiceExtension.responseHook()
- Add type checking to handle different response body types from AWS SDK
middleware:
- String: Already processed by AWS SDK (Uint8ArrayBlobAdapter)
- Uint8Array: Raw binary from AWS (decode with TextDecoder)
- Buffer: Node.js Buffer (convert with toString('utf8'))
- Unexpected types: Log debug message and skip processing gracefully
## Test
1. Reproduced the issue by using latest release with
`InvokeModelWithResponseStream`
```
events: [
{
name: 'exception',
attributes: {
'exception.type': 'ERR_INVALID_ARG_TYPE',
'exception.message': 'The "list" argument must be an instance of SharedArrayBuffer, ArrayBuffer or ArrayBufferView.',
'exception.stacktrace': 'TypeError: The "list" argument must be an instance of SharedArrayBuffer, ArrayBuffer or ArrayBufferView.\n' +
' at TextDecoder.decode (node:internal/encoding:443:16)\n' +
' at BedrockRuntimeServiceExtension.responseHook (/Users/xiami/Documents/workspace/apm/aws-otel-js-instrumentation/sample-applications/simple-express-server/node_modules/@aws/aws-distro-opentelemetry-node-autoinstrumentation/build/src/patches/aws/services/bedrock.js:303:59)\n' +
' at ServicesExtensions.responseHook (/Users/xiami/Documents/workspace/apm/aws-otel-js-instrumentation/sample-applications/simple-express-server/node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/services/ServicesExtensions.js:37:154)\n' +
' at /Users/xiami/Documents/workspace/apm/aws-otel-js-instrumentation/sample-applications/simple-express-server/node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/aws-sdk.js:257:53\n' +
' at process.processTicksAndRejections (node:internal/process/task_queues:95:5)'
},
time: [ 1753815032, 337792 ],
droppedAttributesCount: 0
}
],
links: []
```
2. Tested with the fix and the issue is gone
```
name: 'BedrockRuntime.InvokeModelWithResponseStream',
id: '1d23a4c6f2498f7f',
kind: 2,
timestamp: 1753815031313000,
duration: 687392.625,
attributes: {
'rpc.system': 'aws-api',
'rpc.method': 'InvokeModelWithResponseStream',
'rpc.service': 'BedrockRuntime',
'gen_ai.system': 'aws.bedrock',
'gen_ai.request.model': 'anthropic.claude-3-5-sonnet-20240620-v1:0',
'gen_ai.request.max_tokens': 1000,
'gen_ai.request.temperature': 0.7,
'gen_ai.request.top_p': 0.9,
'aws.is.local.root': false,
```
By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
0 commit comments