Open
Description
Describe the issue
Issue Summary
The example code for InvokeModelWithBidirectionalStreamCommand in the AWS JavaScript SDK v3 documentation does not properly illustrate that the body parameter must be an AsyncIterable object. When users attempt to implement the example as shown, they receive a confusing error: TypeError: this.options.inputStream is not async iterable.
Current Documentation Example:
The current example from https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/InvokeModelWithBidirectionalStreamCommand/ shows:
const input = {
modelId: "STRING_VALUE", // required
body: { // InvokeModelWithBidirectionalStreamInput Union: only one key present
chunk: { // BidirectionalInputPayloadPart
bytes: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
},
},
};
const command = new InvokeModelWithBidirectionalStreamCommand(input);
const response = await client.send(command);
Reproduction Steps
- Create a new Node.js project and install the SDK:
mkdir bedrock-test
cd bedrock-test
npm init -y
npm install @aws-sdk/client-bedrock-runtime
- Create a file test.js with the example code:
import { BedrockRuntimeClient, InvokeModelWithBidirectionalStreamCommand } from "@aws-sdk/client-bedrock-runtime";
const client = new BedrockRuntimeClient({
region: "us-east-1"
// credentials configured via environment variables or ~/.aws/credentials
});
// Following the example from docs
const input = {
modelId: "amazon.nova-sonic-v1:0",
body: { // This is incorrect! body should be an AsyncIterable, not an object
chunk: {
bytes: new TextEncoder().encode(JSON.stringify({
event: {
sessionStart: {
inferenceConfiguration: {
maxTokens: 1024,
temperature: 0.7
}
}
}
}))
}
}
};
const command = new InvokeModelWithBidirectionalStreamCommand(input);
client.send(command).catch(error => {
console.error("Error:", error.message);
});
- Update package.json to include:
{
"type": "module",
"scripts": {
"test": "node test.js"
}
}
- Run the test:
npm run test
- Observe the error:
@aws-sdk/eventstream-handler-node/dist-cjs/index.js:129
throw err;
^
TypeError: this.options.inputStream is not async iterable
at SmithyMessageEncoderStream.asyncIterator (/Users/prettyprettyprettygood/Downloads/bedrock-test/node_modules/@smithy/eventstream-codec/dist-cjs/index.js:458:44)
at asyncIterator.next (<anonymous>)
at MessageEncoderStream.asyncIterator (/Users/prettyprettyprettygood/Downloads/bedrock-test/node_modules/@smithy/eventstream-codec/dist-cjs/index.js:415:22)
at asyncIterator.next (<anonymous>)
at nextAsync (node:internal/streams/from:182:48)
at readable._read (node:internal/streams/from:57:9)
at Readable.read (node:internal/streams/readable:737:12)
at resume_ (node:internal/streams/readable:1255:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Expected Behavior
The documentation should clearly indicate that body must be an AsyncIterable and provide a proper example of how to implement this pattern.
Working Solution
It should be updated to something like below:
const asyncIterableBody = {
[Symbol.asyncIterator]: async function* () {
// Example: Send messages to the model
yield {
chunk: {
bytes: new TextEncoder().encode(JSON.stringify({
//Events that are required
}))
}
};
// You can yield more chunks as needed
}
};
const input = {
modelId: "your-model-id",
body: asyncIterableBody,
};