Skip to content

Raw Bytes breaking AWS Bedrock request.

0

Hi I am trying to pass a pdf into AWS Bedrock. I am following this documentation , but I am getting an error: Can't invoke 'us.meta.llama4-scout-17b-instruct-v1:0'. Reason: Expecting ',' delimiter: line 279 column 22 (char 7332). Which I am interpreting that the pdf file is breaking the JSON format used to send a request. I am able to upload this file into the chat playground and get it to work, but when trying to call it from code it doesn't. I uploaded the file to s3 and accessed it by downloading it down from s3 as well as zipping the file with my code and creating the lambda function off of that so I don't think the file is corrupted. I've also tried converting the document into Base64, but Bedrock no longer recognizes it as a PDF file. Should I only convert the file to Base64 but not the header for the PDF? Thanks in advance for the help :)).

asked a year ago489 views

3 Answers
0

Hello,

I understand you're getting the error "Can't invoke 'us.meta.llama4-scout-17b-instruct-v1:0’. Reason: Expecting ',' delimiter: line 279 column 22 (char 7332)” when trying to process a PDF file in AWS Bedrock.

The error basically occurs because raw PDF data can't be directly embedded in JSON requests. This breaks the JSON formatting, leading to the error you're seeing.



  1. Use Base64 Encoding (For smaller files): Only encode the file content - don't include PDF headers or metadata.

  2. Additionally, you can use S3 as Reference Method (Recommended for larger files):

If the model you’re invoking supports it, you can just pass an S3 URI in the request. This avoids hitting JSON size or encoding issues:

=======
{ "inputDocuments": [ { "s3Uri": "s3://your-bucket-name/path/to/file.pdf" } ] }
=======

If you have any further questions, please reach out to AWS Support [1] and we would be happy to assist you further.

References—

[1] AWS Support - https://docs.aws.amazon.com/awssupport/latest/user/case-management.html#creating-a-support-casehttps://docs.aws.amazon.com/awssupport/latest/user/case-management.html#creating-a-support-case

AWS

answered a year ago

  • Hi, when I try to encode the file in base64, it is no longer detected as a PDF file. However, I cannot add the %PDF-x.x header back in because it is no longer in Base64 format. If possible, could you please provide an example of how you were able to implement a PDF file in Base64 encoding? Thank you! Also the model I am using does not support S3 as a reference method unfortunately.

0

When working with binary files like PDFs in Amazon Bedrock, you need to handle the raw bytes correctly to avoid breaking the JSON format of your request. Here's how to address your issue:

For binary data like PDFs, you should use the "bytes" field in the DocumentSource object, which expects Base64-encoded binary data. When using an AWS SDK, you don't need to manually encode the bytes in base64 - the SDK handles this for you.

The error you're seeing ("Expecting ',' delimiter") suggests that raw binary data is being inserted directly into your JSON request, corrupting the JSON structure.

If you're using an AWS SDK:

  1. Read the PDF file as binary data
  2. Pass the raw binary data directly to the "bytes" field in your DocumentSource
  3. Let the SDK handle the Base64 encoding

If you're making direct API calls without an SDK:

  1. Read the PDF file as binary data
  2. Base64 encode the binary data
  3. Include the encoded string in your request

Don't try to modify the PDF header or structure - the entire file should be treated as binary data. The model will recognize it as a PDF based on the content, not based on how you label it.

Note that Bedrock Agents currently cannot process images through the files parameter, but they can process document files like PDFs for text extraction.
Sources
DocumentSource - Amazon Bedrock
Send images as Base64 files to AWS BedRock Agent | AWS re:Post
Community | How to embed model-specific request payloads when using Amazon Bedrock's InvokeModel API

answered a year ago

0

The error usually means your request payload is no longer valid JSON after you inject the PDF bytes. Two key tips:

When including binary data in a JSON request, Base64-encode only the file contents, not the PDF header or metadata.

Double-check you’re escaping quotes properly — many JSON libraries will choke if the byte data contains unescaped characters.

If you’re using Python or Node, construct the request like:

import base64, json

with open("file.pdf", "rb") as f: encoded = base64.b64encode(f.read()).decode("utf-8")

payload = { "input": { "document": { "name": "file.pdf", "data": encoded, "mimeType": "application/pdf" } } }

print(json.dumps(payload)) # Should serialize cleanly

This ensures the payload is valid JSON and recognized as a PDF.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.