Skip to content

Skip empty text content blocks for Bedrock user messages - #6709

Open
chabinhwang wants to merge 1 commit into
spring-projects:mainfrom
chabinhwang:bedrock-converse-empty-user-text
Open

Skip empty text content blocks for Bedrock user messages#6709
chabinhwang wants to merge 1 commit into
spring-projects:mainfrom
chabinhwang:bedrock-converse-empty-user-text

Conversation

@chabinhwang

Copy link
Copy Markdown
Contributor

Summary

BedrockProxyChatModel always added a text ContentBlock for a UserMessage, even when the message had no text.
The Converse API rejects an empty text content block, so a user message that carries only media failed with an HTTP 400.

The assistant message branch of the same method already guards with StringUtils.hasText(...); this applies the same guard to the user message branch.

Fixes #6695

Reproduction

From the issue: a prompt whose user message has text("") and one image, sent to a vision-capable model through Bedrock, is rejected by the API. The same request without the empty text block succeeds.

UserMessage.builder()
    .text("")
    .media(Media.builder().mimeType(MediaType.IMAGE_PNG).data(pngBytes).build())
    .build()

Testing

Three tests added to BedrockProxyChatModelTest, all going through createRequest, so they assert on the actual ConverseRequest payload:

  • mediaOnlyUserMessageOmitsEmptyTextContentBlock — empty text plus image yields a single image block and no text block
  • blankUserMessageTextOmitsEmptyTextContentBlock — whitespace-only text is treated the same way
  • userMessageWithTextAndMediaKeepsBothContentBlocks — regression guard: text plus image still yields both blocks in order

Verified locally:

  • ./mvnw -Dmaven.build.cache.enabled=false -pl models/spring-ai-bedrock-converse clean packageTests run: 87, Failures: 0, Errors: 0, checkstyle and spring-javaformat pass
  • Reverted the main-code change and re-ran only the three new tests: the two empty-text tests fail (Tests run: 3, Failures: 2) and the regression guard passes, confirming the tests cover the reported defect

BedrockProxyChatModelIT requires AWS credentials and was not executed.

Note on the all-empty case

A user message with neither text nor media now produces a message with an empty content list. That request was already rejected by the API before this change (as an empty text block), so this is not a regression, and it is out of scope here.

The Converse API rejects requests that contain an empty text content
block, so a user message carrying media but no text failed with an
HTTP 400. Only add the text block when the user message actually has
text, matching the guard the assistant message branch already uses.

Fixes spring-projects#6695

Signed-off-by: chabinhwang <7chabin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bedrock Converse: Empty text content is sent to API and being rejected

2 participants