Skip to content

Fix: drop trailing assistant message when rstrip leaves it empty - #8250

Open
Mayuri (mayuriphad) wants to merge 5 commits into
microsoft:mainfrom
mayuriphad:restore-pr-8029
Open

Mayuri (mayuriphad) wants to merge 5 commits into
microsoft:mainfrom
mayuriphad:restore-pr-8029

Conversation

@mayuriphad

Copy link
Copy Markdown

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 trailing AssistantMessage.content was 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.py and test_openai_model_client.py. Ran the full local suite: all pre-existing and new tests pass (unrelated failures due to missing OPENAI_API_KEY in this environment, confirmed unrelated via traceback inspection).\n\n*(This is a restoration of closed PR #8029)*

Copilot AI lite review requested due to automatic review settings September 18, 2026 12:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

_rstrip_last_assistant_message only strips trailing whitespace from the content — it does not remove the assistant message.

2 participants