Skip empty text content blocks for Bedrock user messages - #6709
Open
chabinhwang wants to merge 1 commit into
Open
Skip empty text content blocks for Bedrock user messages#6709chabinhwang wants to merge 1 commit into
chabinhwang wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BedrockProxyChatModelalways added a textContentBlockfor aUserMessage, 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.Testing
Three tests added to
BedrockProxyChatModelTest, all going throughcreateRequest, so they assert on the actualConverseRequestpayload:mediaOnlyUserMessageOmitsEmptyTextContentBlock— empty text plus image yields a single image block and no text blockblankUserMessageTextOmitsEmptyTextContentBlock— whitespace-only text is treated the same wayuserMessageWithTextAndMediaKeepsBothContentBlocks— regression guard: text plus image still yields both blocks in orderVerified locally:
./mvnw -Dmaven.build.cache.enabled=false -pl models/spring-ai-bedrock-converse clean package—Tests run: 87, Failures: 0, Errors: 0, checkstyle andspring-javaformatpassTests run: 3, Failures: 2) and the regression guard passes, confirming the tests cover the reported defectBedrockProxyChatModelITrequires 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.