Skip to content

Bedrock Runtime Converse SDK Document Block does not recognize Uint8Array() when reading PDF files #6774

@Ceebig

Description

@Ceebig

Checkboxes for prior research

Describe the bug

Based on the AWS Bedrock Runtime SDK documentation, if a file is present in a message, it should be passed in as an Uint8Array, and the SDK will convert the Uint8Array into based 64 encoded string.

File upload is handled by middleware multer, I used the readFileSync method to read the PDF file as raw bytes and then convert it to an Uint8Array as shown in the code. However, it keeps emitting the error message Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array. I did a console.log() of the Uint8Array that I created, and I can confirm it is a Uint8Array; the SDK does not seem to recognize it for some reason. Thank you.

Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.
    at toBase64 (/node_modules/@smithy/util-base64/dist-cjs/toBase64.js:15:15)
    at Object.bytes (/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1810:41)

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.8.1

Reproduction Steps

const fs = require("fs");
const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime")
const config = {
  region: "us-east-1",
  credentials: {
    accessKeyId: awsKeyID,
    secretAccessKey: awsKeySecret
  }
}
const bedrockRuntimeClient = new BedrockRuntimeClient(config)

let files = req.files
files.forEach(file => {
      // Read the file synchronously
      const filePath = path.resolve(file.path);
      const rawBytes = fs.readFileSync(filePath);

      userPrompt.content.push({
        document: {
          format: file.originalname.split('.').pop(),
          name: new StringService().sanitizeFileName(file.originalname),
          source: {
            bytes: new Uint8Array(rawBytes),
          }
        }
      })
    })

messages.push(userPrompt)

const input = { // ConverseRequest
     modelId: modelID, // required
     messages: messages,
     inferenceConfig: inferenceConfig,
};

const command = new ConverseCommand(input);
return await bedrockRuntimeClient.send(command)

Observed Behavior

Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.
at toBase64 (/node_modules/@smithy/util-base64/dist-cjs/toBase64.js:15:15)
at Object.bytes (/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1810:41)

Expected Behavior

The AWS SDK should accept the document block with bytes in Uint8Array without any errors

Possible Solution

No response

Additional Information/Context

No response

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.closed-for-stalenessp2This 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