Improve Anthropic Vertex Claude content block handling#789
Conversation
The completePrompt method accessed response.content[0] directly, which could throw a TypeError when the content array is empty, and would return an empty string when the first block is a non-text block (e.g. thinking or tool_use) even when a text block is present later in the array. Use Array.prototype.find() to locate the first text block, matching the pattern already used in anthropic.ts. This safely returns undefined for empty arrays and surfaces the actual text response regardless of its position among content blocks. Add regression tests covering an empty content array and a mixed content array where a thinking block precedes the text block. Signed-off-by: daewoongoh <dw.oh@samsung.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAnthropicVertexHandler.completePrompt now searches the response content array for the first block with type "text" and returns its text, falling back to an empty string if none exists, replacing the previous assumption that the first element was always text. Corresponding tests were added and one test reformatted. ChangesText Block Extraction Fix
Estimated code review effort: 1 (Trivial) | ~5 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Related GitHub Issue
Closes: #788
Description
This PR improves how completion response content blocks are handled when using Claude models through Anthropic Vertex.
The previous implementation only checked
response.content[0], assuming that the first content block would always be atextblock. This works for common text-only responses, but it is less defensive when Claude returns an empty content array or includes non-text blocks, such asthinking, before the actual text response.This change updates the logic to search for the first available
textblock in the response content array. If no text block exists, it safely returns an empty string, preserving the existing fallback behavior.Main changes:
completePromptto find the firsttextblock instead of only checkingresponse.content[0].Test Procedure
Added unit tests for the following cases:
contentarray andcompletePromptreturns an empty string.completePromptreturns the first text block.Test command:
pnpm test src/api/providers/__tests__/anthropic-vertex.spec.tsPre-Submission Checklist
Screenshots / Videos
N/A
Documentation Updates
Additional Notes
This change aligns Anthropic Vertex completion handling with other providers by extracting the first available text block instead of assuming the first content block is always text.
Get in Touch
hehegwk_23849
Summary by CodeRabbit