fix(anthropic): filter empty text blocks from assistant messages - #9491
Closed
ygd58 wants to merge 1 commit into
Closed
fix(anthropic): filter empty text blocks from assistant messages#9491ygd58 wants to merge 1 commit into
ygd58 wants to merge 1 commit into
Conversation
Bedrock and strict Anthropic-compatible endpoints reject messages where a text content block has an empty or whitespace-only 'text' field with: ValidationException: messages: text content blocks must be non-empty This happens in multi-turn conversations when context compression or streaming produces an assistant message with an empty text block alongside tool_use blocks. User messages already had this guard (falls back to '(empty message)'). Assistant messages were missing it — converted_content blocks were extended into the final list without filtering empty text entries. Fix: skip text blocks with empty/whitespace 'text' when building the assistant content block list in convert_to_anthropic_messages(). Fixes NousResearch#9486
1 task
Collaborator
teknium1
reviewed
Jul 12, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for isolating the assistant-list conversion path. The underlying normal-path defect remains on current main: agent/anthropic_adapter.py:1981-1985 converts and extends assistant content-list blocks without filtering blank text, while _convert_content_part_to_anthropic() preserves the original text at agent/anthropic_adapter.py:1733-1739.
Problems
- No regression test accompanies the change.
tests/agent/test_anthropic_adapter.py:1257-1277coverscontent="", which bypasses the list conversion branch; add a list-content case with empty and whitespace-only text alongsidetool_calls. - Current main also has an ordered-replay fast path at
agent/anthropic_adapter.py:1937-1978. It returns blocks before the normal conversion loop, and_sanitize_replay_block()preserves text values atagent/anthropic_adapter.py:1873-1882; a salvage should enforce the same non-empty-text invariant there while retaining signed-block order.
Suggested changes
- Reapply the filter in current
_convert_assistant_message()rather than the pre-refactor location from this branch. - Add normal and ordered-replay regression tests asserting emitted Anthropic text blocks are non-blank.
Automated hermes-sweeper review.
| and not blk.get("text", "").strip() | ||
| ): | ||
| continue # drop empty text block | ||
| blocks.append(blk) |
Contributor
There was a problem hiding this comment.
Please add a regression test for an assistant content list containing empty and whitespace-only text blocks plus tool_calls; the existing empty-string assistant case does not enter this branch.
This was referenced Aug 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #9486
Bedrock and strict Anthropic-compatible endpoints reject messages where a text content block has an empty
textfield:This happens in multi-turn conversations when an assistant message contains an empty text block alongside tool_use blocks.
Root Cause
In
convert_to_anthropic_messages(), user messages already had an empty-block guard. Assistant messages were missing it —converted_contentblocks were extended into the final list without filtering empty text entries.Fix
Skip text blocks with empty/whitespace
textwhen building the assistant content block list:Before / After
Before: Multi-turn chat with tool calls → empty text block in assistant message → Bedrock 400 error
After: Empty text blocks silently dropped → valid message sent → no error