Fix: drop trailing assistant message when rstrip leaves it empty - #8250
Open
Mayuri (mayuriphad) wants to merge 5 commits into
Open
Mayuri (mayuriphad) wants to merge 5 commits into
Mayuri (mayuriphad) wants to merge 5 commits into
Conversation
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.
Fixes #7768
Root cause
_rstrip_last_assistant_message()is documented as removing the last assistant message when it is empty, but it only called.rstrip()on the content and left the (now possibly empty) message in the list. When the trailingAssistantMessage.contentwas whitespace-only, this produced an empty-string content block, which the Anthropic API rejects (text content blocks must be non-empty).Fix
After stripping, if the content becomes an empty string, drop the message entirely — matching the function's documented behavior. Non-empty trailing assistant messages are still only whitespace-stripped, preserving the existing "prefill" behavior and pre-existing passing tests. Same fix applied to both the Anthropic and OpenAI clients, which share an identical copy of this helper.
Verification
Added regression tests in both
test_anthropic_model_client.pyandtest_openai_model_client.py. Ran the full local suite: all pre-existing and new tests pass (unrelated failures due to missingOPENAI_API_KEYin this environment, confirmed unrelated via traceback inspection).\n\n*(This is a restoration of closed PR #8029)*