autogen - 💡(How to fix) Fix _rstrip_last_assistant_message only strips trailing whitespace from the content — it does not remove the assistant message.

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Code Example

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,
        )
RAW_BUFFERClick to expand / collapse

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

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING