Problem/Motivation

When a conversation sent to Anthropic ends with a non-user message (a trailing assistant or system role), the request is rejected:

the conversation must end with a user message (does not support assistant message prefill).

This is hit via the AI Agents multi-agent handoff (e.g. Drupal Canvas AI), which can build a conversation ending in roles "… assistant, system". The user sees a generic "Something went wrong" in Canvas AI and the build fails.

Cause: AnthropicProvider::chat() forwards the messages as-is; Anthropic requires the final message to have the user role.

Steps to reproduce

  1. Configure Anthropic as the provider for AI Agents / Drupal Canvas AI.
  2. Trigger a flow where the handoff ends the conversation with an assistant or system message (e.g. a Canvas AI build).
  3. The request fails with the "must end with a user message" / assistant-prefill error.

Proposed resolution

Override chat() to append a minimal trailing user message (e.g. "Continue.") whenever the last message role is not user (covering both a trailing assistant and system message). MR to follow.

AI usage disclosure (per the Drupal AI contribution policy): AI-Generated: Yes — the patch and this writeup were drafted with AI (Claude) assistance. I, Rajab Natshah, reviewed, tested on Drupal 11.3 / ai_provider_anthropic 1.2.x, and take full responsibility for the correctness, security, and GPL compatibility of this change.

Remaining tasks

  • ✅ File an issue
  • ➖ Addition/Change/Update/Fix
  • ➖ Testing to ensure no regression
  • ➖ Automated unit testing coverage
  • ➖ Automated functional testing coverage
  • ➖ UX/UI designer responsibilities
  • ➖ Readability
  • ➖ Accessibility
  • ➖ Performance
  • ➖ Security
  • ➖ Documentation
  • ➖ Code review by maintainers
  • ➖ Full testing and approval
  • ➖ Credit contributors
  • ➖ Review with the product owner
  • ➖ Release notes snippet
  • ❌ Release

API changes

  • N/A

Data model changes

  • N/A

Release notes snippet

  • N/A
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rajab natshah created an issue. See original summary.

rajab natshah’s picture

Issue summary: View changes

camoa made their first commit to this issue’s fork.

camoa’s picture

Status: Active » Needs review