Skip to content

Skip empty text blocks when building Anthropic messages - #1098

Merged
Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-skip-empty-text-blocks
Sep 18, 2026
Merged

Quim Muntal (qmuntal) merged 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-skip-empty-text-blocks

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

Anthropic rejects empty text blocks. The system-prompt path in buildMessageParams already guards against forwarding a TextContent with empty text (if c.Text != ""), but the message path in buildMessageParam did not — so an assistant or user turn carrying an empty TextContent alongside real content produced an invalid empty text block in the outbound request.

This mirrors the Python client (_prepare_message_for_anthropic), which skips empty text blocks for the same reason.

Change

  • Guard the message-path *message.TextContent case with if c.Text != "", matching the existing system-path guard.

Test

  • TestEmptyTextContentIsSkipped: an assistant turn with an empty TextContent followed by a real one now serializes to a single text block. Fails before the change (2 blocks, one with "text":""), passes after.

Anthropic rejects empty text blocks. The system prompt path already guards
against forwarding a TextContent with empty text, but the message path did
not, so an assistant (or user) turn carrying an empty TextContent alongside
real content produced an invalid empty text block in the request.

Guard the message-path TextContent case the same way, matching the Python
client which skips empty text blocks.
Copilot AI lite review requested due to automatic review settings September 18, 2026 11:19
@github-actions github-actions Bot added area:provider Changes files in the provider area area:provider/anthropic Changes files in the provider / anthropic area size:small At most 30 changed lines across at most 2 files labels Sep 18, 2026

Copilot AI 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.

🟢 Approval recommended

No unresolved review issues remain.

Pull request overview

Prevents invalid empty Anthropic text blocks when building messages.

Changes:

  • Skips empty TextContent values.
  • Adds regression coverage for mixed empty and non-empty text.
File summaries
File Description
provider/anthropicprovider/agent.go Filters empty text blocks.
provider/anthropicprovider/agent_test.go Tests empty text block omission.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions github-actions Bot added kind:code Changes production behavior or code kind:tests Changes tests, fixtures, or test infrastructure labels Sep 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Scope: internal-only (bug fix in unexported buildMessageParam helper; no exported API change)

Changed Go contract: None. provider/anthropicprovider/agent.go's unexported buildMessageParam now skips *message.TextContent with Text == "" when building Anthropic message blocks, mirroring the existing guard already present in the system-prompt path (buildMessageParams). No exported types, functions, or signatures changed.

Upstream evidence reviewed: python/packages/anthropic/agent_framework_anthropic/_chat_client.py, method _prepare_message_for_anthropic (around line 944-957): case "text": # Skip empty text content blocks - Anthropic API rejects them \n if content.text: a_content.append(...). This confirms Python already skips empty text blocks in the message-conversion path, exactly matching the fix applied here.

Result: aligned. This is a straightforward bug fix that brings the Go message-path behavior in line with both the existing Go system-path guard and the upstream Python client's documented behavior (Anthropic API rejects empty text blocks). No divergence, no new public surface, no feature gating concerns. Test coverage (TestEmptyTextContentIsSkipped) is appropriate and mirrors the semantic being fixed.

Generated by Go API Consistency Review Agent · copilot · auto · 21.9 AIC · ⌖ 6.26 AIC · ⊞ 9.2K ·

@qmuntal
Quim Muntal (qmuntal) added this pull request to the merge queue Sep 18, 2026
Merged via the queue into microsoft:main with commit 52b83e5 Sep 18, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider/anthropic Changes files in the provider / anthropic area area:provider Changes files in the provider area kind:code Changes production behavior or code kind:tests Changes tests, fixtures, or test infrastructure size:small At most 30 changed lines across at most 2 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants