fix: disable assistant prefill for Claude 4.6 models#14772
Open
hsuanguo wants to merge 4 commits intoanomalyco:devfrom
Open
fix: disable assistant prefill for Claude 4.6 models#14772hsuanguo wants to merge 4 commits intoanomalyco:devfrom
hsuanguo wants to merge 4 commits intoanomalyco:devfrom
Conversation
Claude Opus 4.6 and Sonnet 4.6 models don't support assistant message prefill across all providers (Anthropic, GitHub Copilot, OpenRouter, etc), causing errors when OpenCode reaches max steps. This fix detects Claude 4.6 models and uses a user message instead of assistant prefill for max steps handling, while preserving prefill for models that support it. Changes: - Add supportsAssistantPrefill() to detect Claude 4.6 models - Update max steps handling to conditionally use user/assistant role - Add comprehensive tests for various providers and model versions Fixes anomalyco#13768
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
…co#13768) The original fix only covered the isLastStep/MAX_STEPS prefill case. Logs revealed the error also occurs in normal conversation flow when any assistant message ends up last in the message array (e.g. from an aborted or interrupted turn). Broader fix: add stripTrailingAssistant() inside normalizeMessages() which is the single choke point for all message processing. This strips any trailing assistant messages for models that don't support prefill (Claude opus-4.6 and sonnet-4.6 across all providers), covering all code paths rather than just the max steps case. Also reverts the prompt.ts role switch since normalizeMessages handles it more correctly at the transport layer.
|
@hsuanguo Has this been fixed yet? This feature is very important to me. |
Author
I haven't seen the error in the last couple of days so I assume it fixes it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #13768
Type of change
What does this PR do?
Problem:
Claude Opus 4.6 and Sonnet 4.6 models reject requests where the last message in the conversation is an assistant message, returning: "This model does not support assistant message prefill. The conversation must end with a user message."
This happens in at least two scenarios:
The issue affects these models across all providers — native Anthropic API, GitHub Copilot, OpenRouter, etc.
Fix:
Added
stripTrailingAssistant()insidenormalizeMessages()inprovider/transform.ts. SincenormalizeMessagesis called from the singleProviderTransform.message()choke point used for all outgoing requests, this covers every code path rather than patching individual call sites.For models that don't support prefill (detected via
supportsAssistantPrefill()), any trailing assistant messages are stripped before the request is sent. For all other models, nothing changes.How did you verify your code works?
Confirmed the root cause using logs from a live session that reproduced the error:
providerID=anthropic,modelID=claude-opus-4-6, URLhttps://api.anthropic.com/v1/messagesrole: assistantfrom a prior interrupted turn — not a max steps prefillAdded integration tests in
ProviderTransform.message - strip trailing assistant for Claude 4.6that verify:Screenshots / recordings
Not applicable - backend logic fix
Checklist