From ed4c7513eae15b570c329d51d4c8117bf78822d4 Mon Sep 17 00:00:00 2001 From: PR Bot Date: Sat, 15 Aug 2026 16:43:24 +0800 Subject: [PATCH] fix: _rstrip_last_assistant_message only strips trailing whitespace from th\n\nFixes #7768 --- .../src/autogen_ext/models/anthropic/_anthropic_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py index 6f68cf7b8cbc..ce4ab1049b54 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/anthropic/_anthropic_client.py @@ -542,10 +542,10 @@ def _rstrip_last_assistant_message(self, messages: Sequence[LLMMessage]) -> Sequ """ Remove the last assistant message if it is empty. """ - # When Claude models last message is AssistantMessage, It could not end with whitespace - if isinstance(messages[-1], AssistantMessage): - if isinstance(messages[-1].content, str): - messages[-1].content = messages[-1].content.rstrip() + # When Claude models last message is AssistantMessage, it must be removed + # (Anthropic API requires conversation to end with a user message) + while messages and isinstance(messages[-1], AssistantMessage): + messages = messages[:-1] return messages