Skip to content

Fix Anthropic requests ending with assistant messages#7781

Open
jdoughty04 wants to merge 1 commit into
microsoft:mainfrom
jdoughty04:codex/anthropic-trailing-assistant-messages
Open

Fix Anthropic requests ending with assistant messages#7781
jdoughty04 wants to merge 1 commit into
microsoft:mainfrom
jdoughty04:codex/anthropic-trailing-assistant-messages

Conversation

@jdoughty04
Copy link
Copy Markdown

@jdoughty04 jdoughty04 commented May 31, 2026

Why are these changes needed?

Anthropic rejects requests when the final conversation turn is an assistant message. The current _rstrip_last_assistant_message helper only strips trailing whitespace from that message, so a non-empty trailing assistant response still gets sent to Anthropic unchanged.

This updates the helper to drop trailing assistant messages before request conversion. It also avoids mutating the caller's original message history and adds coverage for both consecutive trailing assistant messages and assistant messages that appear earlier in the conversation.

Related issue number

Closes #7768

Checks

Local validation:

  • uv run --extra anthropic pytest tests/models/test_anthropic_model_client.py -q
  • uv run ruff format --check src/autogen_ext/models/anthropic/_anthropic_client.py tests/models/test_anthropic_model_client.py
  • uv run ruff check src/autogen_ext/models/anthropic/_anthropic_client.py tests/models/test_anthropic_model_client.py
  • uv run pyright src/autogen_ext/models/anthropic/_anthropic_client.py tests/models/test_anthropic_model_client.py
  • uv run python -m py_compile src/autogen_ext/models/anthropic/_anthropic_client.py
  • git diff --check

@jdoughty04
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@jdoughty04 jdoughty04 marked this pull request as ready for review May 31, 2026 14:48
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.

1 participant