fix: filter empty-content assistant messages before API call#248
Merged
jalehman merged 1 commit intoApr 3, 2026
Merged
Conversation
When tool-use-only assistant turns are stored with content='' and zero message_parts, or when filterNonFreshAssistantToolCalls strips all tool_use blocks from a non-fresh assistant message, the resulting content array is empty ([]) or the content string is falsy. Anthropic (and other providers) reject messages with empty content: 'The content field in the Message object at messages.0 is empty' Add an explicit filter in assemble() to remove these empty assistant messages before passing to sanitizeToolUseResultPairing and the API. The filter only targets assistant messages — user messages with empty content are left untouched (provider may handle differently). Closes Martian-Engineering#238
Contributor
|
Thank you! |
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
Anthropic API rejects messages with empty content:
This occurs when:
content=''and zeromessage_partsin the databasefilterNonFreshAssistantToolCalls()strips all tool_use blocks from a non-fresh assistant message, leavingcontent: []Root Cause
assembler.ts assemble()passesrawMessagesdirectly tosanitizeToolUseResultPairing()and then to the API without validating that assistant messages have non-empty content.While
filterNonFreshAssistantToolCalls()does skip messages wherecontent.length === 0after filtering, this only covers the array case. Messages with falsy string content ('') bypass this check via the!Array.isArray()branch and are passed through unchanged.Fix
Add an explicit filter in
assemble()before the return statement that removes assistant messages with empty content (either[]or falsy string). Only targets assistant role — user messages are left untouched.Testing
Verified on production instance with 5,246+ affected messages across 12 conversations. Filter correctly removes empty assistant messages while preserving all valid messages. All agents recovered immediately after deploying this fix.