-
Notifications
You must be signed in to change notification settings - Fork 12.4k
fix: filter empty content blocks for Bedrock provider #14586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tom-Ryder
wants to merge
2
commits into
anomalyco:dev
Choose a base branch
from
Tom-Ryder:fix/bedrock-empty-content
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,7 @@ export namespace ProviderTransform { | |
| ): ModelMessage[] { | ||
| // Anthropic rejects messages with empty content - filter out empty string messages | ||
| // and remove empty text/reasoning parts from array content | ||
| if (model.api.npm === "@ai-sdk/anthropic") { | ||
| if (model.api.npm === "@ai-sdk/anthropic" || model.api.npm === "@ai-sdk/amazon-bedrock") { | ||
| msgs = msgs | ||
| .map((msg) => { | ||
| if (typeof msg.content === "string") { | ||
|
|
@@ -135,37 +135,31 @@ export namespace ProviderTransform { | |
|
|
||
| if (typeof model.capabilities.interleaved === "object" && model.capabilities.interleaved.field) { | ||
| const field = model.capabilities.interleaved.field | ||
| return msgs.map((msg) => { | ||
| if (msg.role === "assistant" && Array.isArray(msg.content)) { | ||
| const reasoningParts = msg.content.filter((part: any) => part.type === "reasoning") | ||
| const reasoningText = reasoningParts.map((part: any) => part.text).join("") | ||
|
|
||
| // Filter out reasoning parts from content | ||
| const filteredContent = msg.content.filter((part: any) => part.type !== "reasoning") | ||
|
|
||
| // Include reasoning_content | reasoning_details directly on the message for all assistant messages | ||
| if (reasoningText) { | ||
| return { | ||
| ...msg, | ||
| content: filteredContent, | ||
| providerOptions: { | ||
| ...msg.providerOptions, | ||
| openaiCompatible: { | ||
| ...(msg.providerOptions as any)?.openaiCompatible, | ||
| [field]: reasoningText, | ||
| return msgs | ||
| .map((msg) => { | ||
| if (msg.role === "assistant" && Array.isArray(msg.content)) { | ||
|
Comment on lines
+138
to
+140
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change this unrelated stuff? |
||
| const reasoningParts = msg.content.filter((part: any) => part.type === "reasoning") | ||
| const reasoningText = reasoningParts.map((part: any) => part.text).join("") | ||
| const filteredContent = msg.content.filter((part: any) => part.type !== "reasoning") | ||
| if (filteredContent.length === 0) return undefined | ||
| if (reasoningText) { | ||
| return { | ||
| ...msg, | ||
| content: filteredContent, | ||
| providerOptions: { | ||
| ...msg.providerOptions, | ||
| openaiCompatible: { | ||
| ...(msg.providerOptions as any)?.openaiCompatible, | ||
| [field]: reasoningText, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| return { ...msg, content: filteredContent } | ||
| } | ||
|
|
||
| return { | ||
| ...msg, | ||
| content: filteredContent, | ||
| } | ||
| } | ||
|
|
||
| return msg | ||
| }) | ||
| return msg | ||
| }) | ||
| .filter((msg): msg is ModelMessage => msg !== undefined) | ||
| } | ||
|
|
||
| return msgs | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why can't we just have this change +1 test for it, ill cleanup later cause we should be a bit better at doing this across the board but htere was a special case that caused errors in the past