Skip to content

required text for document calls fix - #5675

Merged
akshaydeo merged 2 commits into
devfrom
07-30-required_text_for_document_calls_fix
Jul 30, 2026
Merged

required text for document calls fix#5675
akshaydeo merged 2 commits into
devfrom
07-30-required_text_for_document_calls_fix

Conversation

@akshaydeo

@akshaydeo akshaydeo commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Anthropic rejects messages that contain a document block without an accompanying text block, returning a validation error: "A text block must be included when using documents." This PR fixes that by automatically prepending a single-space placeholder text block whenever a message contains one or more document blocks but no text block.

Changes

  • When converting a Bifrost chat request to an Anthropic request, document-only message content is detected and a minimal placeholder text block (" ") is prepended so the request passes Anthropic's validation.
  • Messages that already include a text block alongside document blocks are left unchanged.
  • Two tests cover both cases: a document-only message receiving the placeholder, and a message with an existing text block not receiving an extra one.

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (React)
  • Docs

How to test

go test ./core/providers/anthropic/...

Expected: all tests pass, including the two new tests:

  • TestToAnthropicChatRequest_DocumentOnlyMessageGetsPlaceholderTextBlock
  • TestToAnthropicChatRequest_DocumentWithTextDoesNotGetPlaceholder

Screenshots/Recordings

N/A

Breaking changes

  • Yes
  • No

Related issues

N/A

Security considerations

None. The placeholder text block is a single space and does not expose any sensitive data.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)
  • I verified the CI pipeline passes locally if applicable

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved Anthropic request formatting for document/file blocks by ensuring a required non-whitespace placeholder is included when no usable text is present.
    • Preserves valid user text when mixed with documents/files and replaces whitespace-only text with the placeholder.
    • Normalizes assistant prefill blocks to keep placeholder placement after any thinking/redacted_thinking sections and avoids trailing whitespace.
  • Tests
    • Added regression tests for document-only messages, mixed text+document inputs, and assistant prefill placeholder/whitespace placement scenarios.

Walkthrough

Anthropic chat request conversion inserts non-whitespace placeholder text for document-only or whitespace-only content, preserves usable text, and places injected text after leading thinking blocks. Regression tests cover user messages and assistant prefills.

Changes

Anthropic document validation

Layer / File(s) Summary
Document placeholder validation
core/providers/anthropic/chat.go, core/providers/anthropic/chat_test.go
Document-only or whitespace-only text content receives a non-whitespace placeholder, while usable text is preserved and document ordering is validated.
Assistant prefill placeholder preservation
core/providers/anthropic/chat.go, core/providers/anthropic/chat_test.go
Assistant prefills restore valid text alongside documents, preserve thinking blocks before injected text, and remove trailing whitespace through helper-backed regression tests.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • maximhq/bifrost#5277: Both changes modify Anthropic content-block assembly around thinking and redacted-thinking ordering.

Suggested reviewers: pratham-mishra04, tejasghatte, sammaji

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and clearly describes the document-call text fix introduced in this PR.
Description check ✅ Passed The PR description includes the required template sections and covers the bug, changes, testing, impact, and security.
✨ Finishing Touches 💡 1
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch 07-30-required_text_for_document_calls_fix
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 07-30-required_text_for_document_calls_fix

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

@akshaydeo
akshaydeo marked this pull request as ready for review July 29, 2026 23:27

akshaydeo commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

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

@akshaydeo akshaydeo mentioned this pull request Jul 29, 2026
8 tasks

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/providers/anthropic/chat.go`:
- Around line 866-870: Update the assistant content normalization around the
hasDocument/hasText handling and the trimming logic near lines 891-903 so
document-only messages retain a single-space text placeholder after trimming.
Re-run the document/text validation after trimming or restore the placeholder
when documents are present, and add a regression test covering an assistant
prefill with a document-only final message.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 68ec2337-5a8a-4208-959d-7212042546d2

📥 Commits

Reviewing files that changed from the base of the PR and between 32db51f and 38d82ad.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go

Comment thread core/providers/anthropic/chat.go Outdated
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch from 38d82ad to a4a4955 Compare July 30, 2026 07:41
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
coderabbitai Bot requested a review from sammaji July 30, 2026 07:42
@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from 32db51f to e27f1d6 Compare July 30, 2026 07:43
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch from a4a4955 to 8f40c80 Compare July 30, 2026 07:43

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/providers/anthropic/chat.go`:
- Around line 874-892: The document-content validation around hasDocument and
hasText must treat whitespace-only text blocks as missing usable text. Use
strings.TrimSpace on text block content before setting hasText, while preserving
non-whitespace text as valid; add a regression test covering a non-final user
document message containing only whitespace text.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7948cfc9-a7ce-4a2e-9524-146727f1731f

📥 Commits

Reviewing files that changed from the base of the PR and between 38d82ad and a4a4955.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/providers/anthropic/chat_test.go

@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from e27f1d6 to ab0f06b Compare July 30, 2026 08:13
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch from 8f40c80 to 51dd24a Compare July 30, 2026 08:13

@coderabbitai coderabbitai Bot left a comment

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 comments (1)
core/providers/anthropic/chat_test.go (1)

176-180: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert non-whitespace, not merely non-empty.

*blocks[0].Text != "" would accept " ", even though the production contract requires non-whitespace text. Use strings.TrimSpace so this regression test catches a whitespace-only placeholder.

Suggested test assertion
-	if blocks[0].Text == nil || *blocks[0].Text == "" {
+	if blocks[0].Text == nil || strings.TrimSpace(*blocks[0].Text) == "" {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/providers/anthropic/chat_test.go` around lines 176 - 180, Update the
placeholder text assertion in the test around blocks[0].Text to trim surrounding
whitespace before checking for emptiness, while preserving the existing nil
check and failure message. Ensure whitespace-only placeholder text is rejected.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@core/providers/anthropic/chat_test.go`:
- Around line 176-180: Update the placeholder text assertion in the test around
blocks[0].Text to trim surrounding whitespace before checking for emptiness,
while preserving the existing nil check and failure message. Ensure
whitespace-only placeholder text is rejected.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 12f201a7-8848-4d26-8029-88c01c8f4042

📥 Commits

Reviewing files that changed from the base of the PR and between 8f40c80 and 51dd24a.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go

@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from ab0f06b to 89e564a Compare July 30, 2026 08:43
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch from 51dd24a to 329e8db Compare July 30, 2026 08:43

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 2

♻️ Duplicate comments (1)
core/providers/anthropic/chat.go (1)

874-892: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Count usable text, not merely text blocks.

Lines 882-883 treat " " and "\t" as valid text. A user or non-final assistant message containing a document plus whitespace-only text therefore skips placeholder insertion and still sends invalid content. Remove/replace whitespace-only text blocks, then insert the placeholder only when no non-whitespace text remains. This repeats the previously reported issue, which is still present.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/providers/anthropic/chat.go` around lines 874 - 892, Update the content
scan around AnthropicContentBlockTypeDocument and AnthropicContentBlockTypeText
to consider text usable only when its contents are non-whitespace. Remove
whitespace-only text blocks before validation, then prepend
documentPlaceholderText when a document has no remaining non-whitespace text;
preserve existing text blocks that contain meaningful content.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/providers/anthropic/chat_test.go`:
- Around line 179-180: Update the placeholder validation in the test assertion
around blocks[0].Text to trim whitespace before checking emptiness, using
strings.TrimSpace. Preserve the nil check and ensure whitespace-only placeholder
text fails the assertion.

In `@core/providers/anthropic/chat.go`:
- Around line 919-927: The placeholder restoration in the trimming logic around
hasAnthropicDocumentBlock must only occur when no other text block contains
non-whitespace text. Preserve existing usable text unchanged; for
whitespace-only blocks alongside documents when usable text exists, remove or
otherwise omit the invalid block instead of replacing it with
documentPlaceholderText.

---

Duplicate comments:
In `@core/providers/anthropic/chat.go`:
- Around line 874-892: Update the content scan around
AnthropicContentBlockTypeDocument and AnthropicContentBlockTypeText to consider
text usable only when its contents are non-whitespace. Remove whitespace-only
text blocks before validation, then prepend documentPlaceholderText when a
document has no remaining non-whitespace text; preserve existing text blocks
that contain meaningful content.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 560dc6ed-aafe-411c-baae-872de1f63d5b

📥 Commits

Reviewing files that changed from the base of the PR and between 51dd24a and 329e8db.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go

Comment thread core/providers/anthropic/chat_test.go Outdated
Comment thread core/providers/anthropic/chat.go Outdated
@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from 89e564a to 4a10a92 Compare July 30, 2026 08:52
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch 2 times, most recently from ff97091 to a05ac19 Compare July 30, 2026 08:59

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/providers/anthropic/chat.go`:
- Around line 874-898: Update the placeholder insertion logic in the Anthropic
content normalization block to preserve leading thinking and redacted_thinking
blocks, inserting the documentPlaceholderText block immediately after them
rather than always prepending at index 0. Add a regression test covering an
assistant prefill containing ReasoningDetails and a document block, verifying
reasoning blocks remain first and the placeholder follows them.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8a7d9109-9540-4736-9213-11f95f5351fa

📥 Commits

Reviewing files that changed from the base of the PR and between ff97091 and a05ac19.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go

Comment thread core/providers/anthropic/chat.go
@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from 4a10a92 to 69305f7 Compare July 30, 2026 09:18
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch 2 times, most recently from e64abde to 0290b4f Compare July 30, 2026 09:32
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 30, 2026
@akshaydeo
akshaydeo force-pushed the 07-30-required_text_for_document_calls_fix branch from 0290b4f to 194b987 Compare July 30, 2026 09:40
@akshaydeo
akshaydeo force-pushed the 07-30-recalculate_cost_fixes branch from 69305f7 to 78d8e96 Compare July 30, 2026 09:40

akshaydeo commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Merge activity

  • Jul 30, 9:40 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 30, 9:42 AM UTC: @akshaydeo merged this pull request with Graphite.

@akshaydeo
akshaydeo changed the base branch from 07-30-recalculate_cost_fixes to graphite-base/5675 July 30, 2026 09:41
@akshaydeo
akshaydeo changed the base branch from graphite-base/5675 to dev July 30, 2026 09:41
@akshaydeo
akshaydeo dismissed coderabbitai[bot]’s stale review July 30, 2026 09:41

The base branch was changed.

@akshaydeo
akshaydeo merged commit d254264 into dev Jul 30, 2026
8 of 10 checks passed
@akshaydeo
akshaydeo deleted the 07-30-required_text_for_document_calls_fix branch July 30, 2026 09:42

@coderabbitai coderabbitai Bot left a comment

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 comments (1)
core/providers/anthropic/chat.go (1)

14-21: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Update the placeholder comment to match its insertion behavior.

The comment says the placeholder is “prepended,” but the implementation inserts it after leading reasoning blocks. Describe it as “inserted” and mention the ordering constraint to avoid future regressions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/providers/anthropic/chat.go` around lines 14 - 21, Update the comment
for documentPlaceholderText to describe the placeholder as being inserted rather
than prepended, and state that it is inserted after leading reasoning blocks to
preserve their ordering.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@core/providers/anthropic/chat.go`:
- Around line 14-21: Update the comment for documentPlaceholderText to describe
the placeholder as being inserted rather than prepended, and state that it is
inserted after leading reasoning blocks to preserve their ordering.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9afab188-c460-4b61-a097-ce8985002e82

📥 Commits

Reviewing files that changed from the base of the PR and between 0290b4f and 194b987.

📒 Files selected for processing (2)
  • core/providers/anthropic/chat.go
  • core/providers/anthropic/chat_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/providers/anthropic/chat_test.go

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.

2 participants