Skip to content

feat: add text block support for Bedrock Anthropic messages#86

Closed
Pratham-Mishra04 wants to merge 1 commit intographite-base/86from
06-15-fix_bedrock_text_handling_bug_fixes
Closed

feat: add text block support for Bedrock Anthropic messages#86
Pratham-Mishra04 wants to merge 1 commit intographite-base/86from
06-15-fix_bedrock_text_handling_bug_fixes

Conversation

@Pratham-Mishra04
Copy link
Copy Markdown
Collaborator

Added support for text content blocks in Bedrock Anthropic chat completions. This change allows the provider to properly handle text blocks within content blocks, ensuring they are included in the message content sent to the Bedrock API.

Copy link
Copy Markdown
Collaborator Author

Pratham-Mishra04 commented Jun 15, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@akshaydeo akshaydeo marked this pull request as ready for review June 15, 2025 06:10
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 15, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability by ensuring that empty text content is not included in chat messages for Anthropic models.

Summary by CodeRabbit

  • Bug Fixes
    • Resolved an issue where empty or missing text content in chat messages could be incorrectly processed, improving the reliability of chat interactions with Anthropic models.

Walkthrough

A conditional check was added in the prepareChatCompletionMessages method for Anthropic models within the Bedrock provider. The code now ensures that only content blocks with a non-nil Text field are appended as text messages, preventing the inclusion of messages with nil text pointers.

Changes

File(s) Change Summary
core/providers/bedrock.go Added a nil check for the Text field in content blocks before appending them as text messages in Anthropic chat message preparation.

Poem

In Bedrock’s code, a check appears,
To guard against those nil-text fears.
Only words that truly show,
Will in the message content flow.
Now rabbits code with peace of mind,
No empty texts for us to find!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch 06-15-fix_bedrock_text_handling_bug_fixes
  • Post Copyable Unit Tests in Comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ BugBot reviewed your changes and found no bugs!


BugBot free trial expires on June 17, 2025
You have used $0.00 of your $50.00 spend limit so far. Manage your spend limit in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2791720 and d06a1d9.

📒 Files selected for processing (1)
  • core/providers/bedrock.go (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Pratham-Mishra04
PR: maximhq/bifrost#55
File: core/providers/anthropic.go:526-550
Timestamp: 2025-06-04T09:29:46.287Z
Learning: In core/providers/anthropic.go, the content field in formattedMessages is always of type []interface{} because it's explicitly constructed that way upstream in the prepareAnthropicChatRequest function. Defensive type casting for multiple types is not needed since the type is guaranteed by the construction logic.
core/providers/bedrock.go (1)
Learnt from: Pratham-Mishra04
PR: maximhq/bifrost#55
File: core/providers/anthropic.go:526-550
Timestamp: 2025-06-04T09:29:46.287Z
Learning: In core/providers/anthropic.go, the content field in formattedMessages is always of type []interface{} because it's explicitly constructed that way upstream in the prepareAnthropicChatRequest function. Defensive type casting for multiple types is not needed since the type is guaranteed by the construction logic.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Graphite / mergeability_check
  • GitHub Check: Graphite / mergeability_check

@Pratham-Mishra04 Pratham-Mishra04 force-pushed the 06-15-fix_bedrock_text_handling_bug_fixes branch from d06a1d9 to efd98ff Compare June 15, 2025 14:08
@Pratham-Mishra04 Pratham-Mishra04 force-pushed the 06-15-enhancement_added_support_for_extra_headers_at_provider_level branch from 2791720 to cb71ba7 Compare June 15, 2025 14:08
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d06a1d9 and efd98ff.

📒 Files selected for processing (1)
  • core/providers/bedrock.go (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: Pratham-Mishra04
PR: maximhq/bifrost#55
File: core/providers/anthropic.go:526-550
Timestamp: 2025-06-04T09:29:46.287Z
Learning: In core/providers/anthropic.go, the content field in formattedMessages is always of type []interface{} because it's explicitly constructed that way upstream in the prepareAnthropicChatRequest function. Defensive type casting for multiple types is not needed since the type is guaranteed by the construction logic.
core/providers/bedrock.go (1)
Learnt from: Pratham-Mishra04
PR: maximhq/bifrost#55
File: core/providers/anthropic.go:526-550
Timestamp: 2025-06-04T09:29:46.287Z
Learning: In core/providers/anthropic.go, the content field in formattedMessages is always of type []interface{} because it's explicitly constructed that way upstream in the prepareAnthropicChatRequest function. Defensive type casting for multiple types is not needed since the type is guaranteed by the construction logic.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Graphite / mergeability_check
  • GitHub Check: Graphite / mergeability_check

Comment thread core/providers/bedrock.go
Comment on lines +522 to +527
if block.Text != nil {
content = append(content, BedrockAnthropicTextMessage{
Type: "text",
Text: *block.Text,
})
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Guard against empty or whitespace-only text blocks
While the new nil check prevents pointer dereferences, empty strings still generate no-op messages. Filter out blocks with empty or whitespace-only text to keep the payload clean.

Apply this diff:

@@ -521,7 +521,8 @@
-                       if block.Text != nil {
+                       if block.Text != nil && strings.TrimSpace(*block.Text) != "" {
                            content = append(content, BedrockAnthropicTextMessage{
                                Type: "text",
                                Text: *block.Text,
                            })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if block.Text != nil {
content = append(content, BedrockAnthropicTextMessage{
Type: "text",
Text: *block.Text,
})
}
if block.Text != nil && strings.TrimSpace(*block.Text) != "" {
content = append(content, BedrockAnthropicTextMessage{
Type: "text",
Text: *block.Text,
})
}
🤖 Prompt for AI Agents
In core/providers/bedrock.go around lines 522 to 527, the code appends text
messages even if the text is empty or contains only whitespace. To fix this, add
a check to ensure block.Text is not nil and that the dereferenced string is not
empty or whitespace-only before appending the message. Use a string trimming
function to verify the text content is meaningful and skip appending if it is
empty after trimming.

@Pratham-Mishra04 Pratham-Mishra04 changed the base branch from 06-15-enhancement_added_support_for_extra_headers_at_provider_level to graphite-base/86 June 15, 2025 14:35
@akshaydeo akshaydeo deleted the 06-15-fix_bedrock_text_handling_bug_fixes branch August 31, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant