Skip empty text blocks when building Anthropic messages - #1098
Quim Muntal (qmuntal) merged 1 commit into
Conversation
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.
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved review issues remain.
Pull request overview
Prevents invalid empty Anthropic text blocks when building messages.
Changes:
- Skips empty
TextContentvalues. - 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.
|
Scope: internal-only (bug fix in unexported Changed Go contract: None. Upstream evidence reviewed: 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 (
|
Anthropic rejects empty text blocks. The system-prompt path in
buildMessageParamsalready guards against forwarding aTextContentwith empty text (if c.Text != ""), but the message path inbuildMessageParamdid not — so an assistant or user turn carrying an emptyTextContentalongside 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
*message.TextContentcase withif c.Text != "", matching the existing system-path guard.Test
TestEmptyTextContentIsSkipped: an assistant turn with an emptyTextContentfollowed by a real one now serializes to a single text block. Fails before the change (2 blocks, one with"text":""), passes after.