Skip to content

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

@shivase

Description

@shivase

What happened?

Describe the bug
_rstrip_last_assistant_message only strips trailing whitespace from the content — it does not remove the assistant message. So when the message history genuinely ends with a non-empty assistant message, it passes through unchanged and Anthropic rejects it.

Claude Recommend Fix

        from autogen_ext.models.anthropic import AnthropicChatCompletionClient

        class _AnthropicClient(AnthropicChatCompletionClient):
            # autogen-ext 0.7.x only whitespace-strips trailing assistant messages;
            # Anthropic API requires the conversation to end with a user message.
            def _rstrip_last_assistant_message(self, messages: Sequence[_LLMMessage]) -> Sequence[_LLMMessage]:
                msgs = list(messages)
                while msgs and isinstance(msgs[-1], _AssistantMessage):
                    msgs.pop()
                return msgs

        return _AnthropicClient(
            model=model_name,
            api_key=llm_key,
        )

Which packages was the bug in?

Python Extensions (autogen-ext)

AutoGen library version.

Python 0.7.5

Other library version.

No response

Model used

anthropic/claude-sonnet-4-6

Model provider

Anthropic

Other model provider

No response

Python version

None

.NET version

None

Operating system

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions