fix: #3607044 Append a trailing user message when a conversation ends with a non-user role

Fixes #3607044.

AnthropicProvider::chat() forwards messages as-is; Anthropic rejects a conversation whose last message is not from the user ("the conversation must end with a user message" / no assistant prefill). The AI Agents multi-agent handoff (e.g. Drupal Canvas AI) can end a conversation with a trailing assistant or system message, producing a generic "Something went wrong" in Canvas AI.

This overrides chat() to append a minimal trailing user message ("Continue.") whenever the last message role is not user (covering both trailing assistant and system).

Tested on Drupal 11.3, ai_provider_anthropic 1.2.x with ai_agents + Drupal Canvas AI: the prefill error no longer occurs, normal chat unaffected.


AI usage disclosure (per https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquette/policy-on-the-use-of-ai-when-contributing-to-drupal): AI-Generated: Yes — the patch and this writeup were drafted with AI (Claude) assistance. I, Rajab Natshah, reviewed, tested, and take full responsibility for the correctness, security, and GPL compatibility of this change.

Checkpoints:

  • 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
  • Reviewed by a human
  • Code review by maintainers
  • Full testing and approval
  • Credit contributors
  • Review with the product owner
  • Release notes snippet
  • Release
Edited by Rajab Natshah

Merge request reports

Loading