Skip to content

fix: Anthropic rejects empty text blocks in assistant history#559

Merged
tlongwell-block merged 1 commit into
mainfrom
fix/anthropic-empty-text-block
May 12, 2026
Merged

fix: Anthropic rejects empty text blocks in assistant history#559
tlongwell-block merged 1 commit into
mainfrom
fix/anthropic-empty-text-block

Conversation

@tlongwell-block
Copy link
Copy Markdown
Collaborator

Problem

Anthropic's Messages API rejects {"type": "text", "text": ""} with:

400 Bad Request: messages: text content blocks must be non-empty

When an assistant turn has no text and no tool calls (rare but possible — e.g. empty end_turn response), the serializer produces an empty text block as a fallback to satisfy the non-empty content array requirement. This creates a permanent 400 loop: the corrupted history is preserved in the session, so every subsequent prompt on that session sends the same invalid payload and gets the same rejection.

With the session-preservation fix from #544, this is worse — the harness correctly keeps the session alive (pipe is intact), but the session is now permanently poisoned. The event gets requeued with exponential backoff up to 10 times, all failing identically.

Fix

Use " " (single space) instead of "" in the empty-content fallback. Satisfies both Anthropic constraints: non-empty content array and non-empty text block.

Changes

crates/sprout-agent/src/llm.rs: 1 character changed (""" "), plus a comment explaining why.

…turns

Anthropic's Messages API rejects text content blocks with empty strings
("messages: text content blocks must be non-empty"). When an assistant
turn has no text and no tool calls, the serializer produced text: ""
which causes a permanent 400 loop — the session is unrecoverable
because every retry sends the same invalid history.

Use a single space instead, satisfying both Anthropic constraints:
non-empty content array and non-empty text block.
@tlongwell-block tlongwell-block merged commit 69734bf into main May 12, 2026
15 checks passed
@tlongwell-block tlongwell-block deleted the fix/anthropic-empty-text-block branch May 12, 2026 23:41
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.

1 participant