openclaw - 💡(How to fix) Fix [Bug] Anthropic prefill error surfaces to user after context compaction (unmatched error pattern + missing trailing-assistant guard) [1 comments, 1 participants]

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…
GitHub stats
openclaw/openclaw#54410Fetched 2026-04-08 01:27:57
View on GitHub
Comments
1
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
closed ×1commented ×1cross-referenced ×1locked ×1

After context compaction, users see raw API errors:

LLM request rejected: This model does not support assistant message prefill. The conversation must end with a user message.

This was initially reported as a plugin issue (larksuite/openclaw-lark#285), but the plugin team identified it as an OpenClaw SDK issue.

Error Message

But does not check for or remove a trailing assistant turn. This causes Anthropic to reject the request with the prefill error.

2. Unrecognized error pattern

The SDK's role-ordering error recovery appears to match: But Anthropic returns a different error message for this case: Since this pattern isn't matched, the error bypasses the existing fallback/recovery logic and surfaces directly to the user. 2. The error recovery pattern should also match "does not support assistant message prefill" or "conversation must end with a user message"

Root Cause

Root Cause Analysis (from plugin team)

Code Example

LLM request rejected: This model does not support assistant message prefill. The conversation must end with a user message.

---

"incorrect role information|roles must alternate"

---

"This model does not support assistant message prefill. The conversation must end with a user message."
RAW_BUFFERClick to expand / collapse

Summary

After context compaction, users see raw API errors:

LLM request rejected: This model does not support assistant message prefill. The conversation must end with a user message.

This was initially reported as a plugin issue (larksuite/openclaw-lark#285), but the plugin team identified it as an OpenClaw SDK issue.

Root Cause Analysis (from plugin team)

Two potential gaps in the SDK:

1. Missing trailing-assistant-message guard after compaction

When buildSessionContext() rebuilds the conversation from the compaction summary + kept tail, it may produce a context that ends with an assistant message.

validateAnthropicTurns appears to only:

  • Merge consecutive user turns
  • Strip dangling tool_use blocks

But does not check for or remove a trailing assistant turn. This causes Anthropic to reject the request with the prefill error.

2. Unrecognized error pattern

The SDK's role-ordering error recovery appears to match:

"incorrect role information|roles must alternate"

But Anthropic returns a different error message for this case:

"This model does not support assistant message prefill. The conversation must end with a user message."

Since this pattern isn't matched, the error bypasses the existing fallback/recovery logic and surfaces directly to the user.

Expected Behavior

  1. buildSessionContext() should ensure the rebuilt context never ends with an assistant message after compaction
  2. The error recovery pattern should also match "does not support assistant message prefill" or "conversation must end with a user message"

Environment

  • OpenClaw: 2026.3.23-2
  • Plugin: @larksuite/[email protected]
  • Model: anthropic/claude-sonnet-4-6
  • Trigger: Long conversation that triggers context compaction

Reference

Plugin team analysis: https://github.com/larksuite/openclaw-lark/issues/285#issuecomment-4125216163

extent analysis

Fix Plan

To address the issue, we need to modify the buildSessionContext() function to ensure the rebuilt context never ends with an assistant message after compaction. We also need to update the error recovery pattern to match the new error message.

Step 1: Update buildSessionContext() function

function buildSessionContext(compactionSummary, keptTail) {
  // Rebuild conversation context
  const context = rebuildContext(compactionSummary, keptTail);
  
  // Check if context ends with an assistant message
  if (context.turns[context.turns.length - 1].role === 'assistant') {
    // Remove trailing assistant turn
    context.turns.pop();
    
    // Add a new user turn to ensure context ends with a user message
    context.turns.push({ role: 'user', message: '' });
  }
  
  return context;
}

Step 2: Update error recovery pattern

const errorPatterns = [
  // Existing pattern
  'incorrect role information|roles must alternate',
  // New pattern to match Anthropic error message
  'does not support assistant message prefill|conversation must end with a user message',
];

function handleError(error) {
  const errorMessage = error.message;
  for (const pattern of errorPatterns) {
    if (errorMessage.match(pattern)) {
      // Trigger fallback/recovery logic
      return recoverFromError(error);
    }
  }
  // If no pattern matches, surface error to user
  return errorMessage;
}

Verification

To verify the fix, test the buildSessionContext() function with a sample compaction summary and kept tail that would previously produce a context ending with an assistant message. Ensure the rebuilt context now ends with a user message.

Additionally, test the error recovery pattern with the new Anthropic error message to ensure it triggers the fallback/recovery logic.

Extra Tips

  • Make sure to update the OpenClaw SDK version to include these changes.
  • Consider adding more test cases to cover different scenarios and ensure the fix is robust.
  • Review the plugin team analysis and reference materials to ensure a thorough understanding of the issue and fix.

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

openclaw - 💡(How to fix) Fix [Bug] Anthropic prefill error surfaces to user after context compaction (unmatched error pattern + missing trailing-assistant guard) [1 comments, 1 participants]