aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

ValidationException: Field required for CachePoint ContentBlock in AWS SDK for Java 2.32.2

Open ilove2git opened this issue 5 months ago • 6 comments

Describe the bug

When using AWS SDK for Java version 2.32.2 with Amazon Bedrock Runtime, adding a CachePoint ContentBlock to a message results in a ValidationException with the error message: messages.0.content.2.type: Field required.

Error Details

software.amazon.awssdk.services.bedrockruntime.model.ValidationException: 
The model returned the following errors: messages.0.content.2.type: Field required 
(Service: BedrockRuntime, Status Code: 400, Request ID: 1ff03d35-55e1-4582-aefe-42600fbcb2df)

Regression Issue

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

Expected Behavior

The CachePoint ContentBlock should be accepted as a valid content block in the message, allowing for prompt caching functionality.

Current Behavior

The API returns a ValidationException indicating that a type field is required for the third ContentBlock (index 2), which is the CachePoint block.

Reproduction Steps

  1. Set up Maven project with AWS SDK for Java 2.32.2:

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>bedrockruntime</artifactId>
        <version>2.32.2</version>
    </dependency>
    
  2. Create a BedrockRuntimeClient:

    BedrockRuntimeClient client = BedrockRuntimeClient.builder()
        .region(Region.US_EAST_1)
        .build();
    
  3. Prepare a PDF document (any PDF file will work for reproduction)

  4. Create ContentBlocks with CachePoint:

    List<ContentBlock> messageContent = Arrays.asList(
        ContentBlock.builder().text("Analyze this document").build(),
        ContentBlock.builder()
            .document(DocumentBlock.builder()
                .format("pdf")
                .name("test-doc")
                .source(DocumentSource.builder()
                    .bytes(SdkBytes.fromByteArray(pdfBytes))
                    .build())
                .build())
            .build(),
        ContentBlock.builder()
            .cachePoint(CachePointBlock.builder()
                .type(CachePointType.DEFAULT)
                .build())
            .build()
    );
    
  5. Create and send ConverseRequest:

    ConverseRequest request = ConverseRequest.builder()
        .modelId("us.anthropic.claude-3-7-sonnet-20250219-v1:0")
        .messages(Message.builder()
            .role(ConversationRole.USER)
            .content(messageContent)
            .build())
        .build();
    
    client.converse(request); // This will throw ValidationException
    
  6. Observe the error: The call will fail with ValidationException: messages.0.content.2.type: Field required

Note: The same code works perfectly when removing the CachePoint ContentBlock (using only text and document blocks).

Possible Solution

  1. Check if ContentBlock requires an explicit type field when using CachePoint
  2. Verify if CachePointBlock structure has changed in recent SDK versions
  3. Confirm if there are additional required fields for CachePoint ContentBlocks

Additional Information/Context

  • The same code structure works perfectly when only using text and document ContentBlocks
  • The error specifically mentions messages.0.content.2.type: Field required, suggesting the CachePoint ContentBlock is missing a required type field
  • This appears to be related to the ContentBlock serialization/validation process

AWS Java SDK version used

2.32.2

JDK version used

openjdk version "21.0.6" 2025-01-21 LTS OpenJDK Runtime Environment Corretto-21.0.6.7.1 (build 21.0.6+7-LTS) OpenJDK 64-Bit Server VM Corretto-21.0.6.7.1 (build 21.0.6+7-LTS, mixed mode, sharing)

Operating System and version

mac os

ilove2git avatar Jul 18 '25 03:07 ilove2git

Hi @ilove2git,

Thanks for reporting the issue. Prompt Caching support feature for Converse API was introduced in Java SDK v2.31.12. I can confirm that the ValidationException you encountered in the described scenario is reproducible from then, up until the most recent available version 2.32.4.

I am further checking on this with the Bedrock service team and shall get back to you as soon as I have an update on it.

Regards, Chaitanya

bhoradc avatar Jul 21 '25 21:07 bhoradc

I can confirm that this also happens with the Python SDK (boto3/botocore 1.40.0).

twall avatar Aug 01 '25 19:08 twall

Given that the error comes from amazons back-end, I suspect that this happens with all the SDKs.

@bhoradc Any update on this?

mm-salesqueze avatar Aug 12 '25 16:08 mm-salesqueze

This appears to only happen if the cache point immediately follows the document. If there is a text message between the two it works.

mm-salesqueze avatar Aug 12 '25 16:08 mm-salesqueze

To help python users googling the issue, the error message in python is: botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ConverseStream operation: The model returned the following errors: messages.0.content.2.type: Field required

This appears to only happen if the cache point immediately follows the document. If there is a text message between the two it works.

We noticed the same so have been putting document content before text content as a workaround.

carljeske avatar Aug 13 '25 08:08 carljeske

I also encountered exactly the same issue when using Golang, and the trigger condition is also when the cache point follows a document block.

hzpbuaa-dot avatar Sep 17 '25 16:09 hzpbuaa-dot