Skip to content

Improve Anthropic Vertex Claude content block handling#789

Open
daewoongoh wants to merge 1 commit into
Zoo-Code-Org:mainfrom
daewoongoh:fix/anthropic-vertex-complete-prompt-safe-access
Open

Improve Anthropic Vertex Claude content block handling#789
daewoongoh wants to merge 1 commit into
Zoo-Code-Org:mainfrom
daewoongoh:fix/anthropic-vertex-complete-prompt-safe-access

Conversation

@daewoongoh

@daewoongoh daewoongoh commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

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 a text block. 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 as thinking, before the actual text response.

This change updates the logic to search for the first available text block in the response content array. If no text block exists, it safely returns an empty string, preserving the existing fallback behavior.

Main changes:

  • Updated Anthropic Vertex completePrompt to find the first text block instead of only checking response.content[0].
  • Safely handles empty content arrays.
  • Added test coverage for empty Claude content responses.
  • Added test coverage for mixed Claude content responses where a non-text block appears before a text block.

Test Procedure

Added unit tests for the following cases:

  1. Claude returns an empty content array and completePrompt returns an empty string.
  2. Claude returns mixed content blocks and completePrompt returns the first text block.

Test command:

pnpm test src/api/providers/__tests__/anthropic-vertex.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

N/A

Documentation Updates

  • No documentation updates are required.

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

  • Bug Fixes
    • Improved prompt completion handling for Claude/Vertex responses so text is extracted correctly even when non-text content appears first.
    • Empty responses now return an empty string instead of failing or returning unexpected output.
    • Added test coverage for empty content and mixed content responses to prevent regressions.

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>
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6d0a1f2f-3706-488e-87a2-5d0ae0b17416

📥 Commits

Reviewing files that changed from the base of the PR and between 1c728e7 and 2e8850e.

📒 Files selected for processing (2)
  • src/api/providers/__tests__/anthropic-vertex.spec.ts
  • src/api/providers/anthropic-vertex.ts

📝 Walkthrough

Walkthrough

AnthropicVertexHandler.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.

Changes

Text Block Extraction Fix

Layer / File(s) Summary
Safe text block extraction
src/api/providers/anthropic-vertex.ts
completePrompt now finds the first "text"-typed content block instead of indexing the first element, returning "" if none is found.
Test coverage for edge cases
src/api/providers/__tests__/anthropic-vertex.spec.ts
Added tests for empty content array and mixed thinking/text content blocks; reformatted an unrelated test call for readability.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested labels: awaiting-review

Suggested reviewers: taltas, JamesRobert20, navedmerchant, hannesrudolph, edelauna

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the provider-specific Claude content block handling fix.
Description check ✅ Passed The PR description follows the template and covers the issue link, change summary, testing, checklist, screenshots, notes, and docs status.
Linked Issues check ✅ Passed The code and tests match issue #788 by finding the first text block and returning an empty string for empty or textless content.
Out of Scope Changes check ✅ Passed The changes stay focused on Anthropic Vertex parsing and regression tests, with no unrelated scope added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot added the awaiting-review PR changes are ready and waiting for maintainer re-review label Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review PR changes are ready and waiting for maintainer re-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT] Improve Anthropic Vertex Claude completion parsing for non-text content blocks

1 participant