Skip to content

Documentation Issue: InvokeModelWithBidirectionalStreamCommand example causes "this.options.inputStream is not async iterable" error #7125

Open
@prettyprettyprettygood

Description

@prettyprettyprettygood

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

  1. 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
  1. 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);
});
  1. Update package.json to include:
{
  "type": "module",
  "scripts": {
    "test": "node test.js"
  }
}
  1. Run the test:
npm run test
  1. 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, 
};

Links

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/InvokeModelWithBidirectionalStreamCommand/

Metadata

Metadata

Assignees

Labels

documentationThis is a problem with documentation.p2This is a standard priority issueresponse-requestedWaiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions