Bedrock Claude 3.5 Sonnet v2 is not supporting new attachments (PDF)
Problem
I use Anthropic's Claude models via Amazon Bedrock. I wanted to try the new model `Claude 3.5 Sonnet v2` with PDF attachment support but it's not working. I am using Python and tried using both libraries `boto3` (`bedrock-runtime` client) and `anthropic.AnthropicBedrock`. I could not find any documentation given by either AWS or Anthropic on how to attach PDF file so I'm just doing hit and trials. Boto For example, I tried using Bedrock playground and it works on UI. I exported the messages as JSON (see screenshot) and then coded the same in python using `boto`. <img width="1443" alt="image" src="https://github.com/user-attachments/assets/efae6031-e4ea-45f5-81ca-934a7156118a"> [code block] It gives me this error: [code block] Maybe AWS needs to add this support in their validator. Anthropic I also tried using Anthropic SDK for Python with `AnthropicBedrock`. Took help from the code of this PR #721 and coded in python [code block] It gives me this error: [code block]
Error Output
Exception: An error occurred (ValidationException) when calling the InvokeModel operation: messages.0.content.1: Input tag 'document' found using 'type' does not match any of the expected tags: 'text', 'image',
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement PDF Attachment Support for Claude 3.5 Sonnet v2
The error occurs because the API expects specific input tags for messages, and the PDF attachment is not recognized as a valid type. The current implementation is sending a 'document' type which is not supported. The API documentation does not specify how to handle PDF attachments, leading to confusion and validation errors.
Awaiting Verification
Be the first to verify this fix
- 1
Check API Documentation for Supported Formats
Review the latest API documentation from both AWS Bedrock and Anthropic to confirm the supported input formats for the Claude 3.5 Sonnet v2 model. Ensure that PDF attachments are explicitly mentioned as supported.
- 2
Use Correct Input Format for PDF
Modify the input structure to match the expected format. If the API does not support PDF directly, consider converting the PDF content to text or images and sending them as 'text' or 'image' types instead.
pythoninput_data = { 'messages': [ {'role': 'user', 'content': 'Here is the text extracted from the PDF.'}, {'role': 'user', 'content': 'image_data_here', 'type': 'image'} ] } - 3
Update Your Python Code
Ensure your Python code uses the correct input format. Replace the PDF attachment with the extracted text or images as shown in the previous step. This will prevent the validation error from occurring.
pythonresponse = bedrock.invoke_model( ModelId='claude-3.5-sonnet-v2', Body=json.dumps(input_data), ContentType='application/json' ) - 4
Test the Implementation
Run your updated Python code to test if the model processes the input correctly without throwing validation errors. Monitor the response for expected output.
- 5
Log and Review Errors
If errors persist, log the full error response and review it for additional clues. This can help identify if further adjustments are needed in the input structure.
pythonprint(response['Error'])
Validation
Confirm the fix by running the updated code and checking for successful responses from the Claude 3.5 Sonnet v2 model. Ensure that the output is as expected and does not contain validation errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep