fix(transform): skip trailing assistant messages in applyCaching for GitHub Copilot#29609
Conversation
…GitHub Copilot When using Claude models via the GitHub Copilot gateway, applyCaching was stamping copilot_cache_control providerOptions onto the last two non-system messages, including any trailing assistant message. The Copilot gateway translates these options into a trailing assistant prefill block, which Claude Sonnet 4.5+ strictly rejects with: 400 Bad Request: This model does not support assistant message prefill. The conversation must end with a user message. The fix filters out assistant messages from the caching candidates when the provider is @ai-sdk/github-copilot, so only user/tool messages receive cache-control options. Note: root cause was identified through AI-assisted static analysis of this file. The causal chain (providerOptions → gateway prefill translation → 400) has not been independently verified against the Copilot gateway source. The fix is minimal and conservative — it only restricts behaviour for the github-copilot provider and does not affect other providers.
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
The following comment was made by an LLM, it may be inaccurate: Found some potentially related PRs: PR #14772: fix: disable assistant prefill for Claude 4.6 models
PR #26477: fix(provider): keep Claude prefill requests user-final
These PRs appear to address the same root issue (Claude rejecting assistant message prefills) but may target different providers or scopes. Worth reviewing to avoid overlapping fixes or ensure consistency in the approach. |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Problem
When using Claude models via the GitHub Copilot gateway (e.g.
github-copilot/claude-sonnet-4.6),applyCachingstampscopilot_cache_controlproviderOptions onto the last two non-system messages, regardless of their role. If the last non-system message is an assistant message, the Copilot gateway translates those options into a trailing assistant prefill block in the HTTP payload, which Claude Sonnet 4.5+ rejects with:This is intermittent — it only triggers when the last message in the message array is an assistant message, which can occur (e.g. after certain compaction or tool-call sequences).
Fix
Filter out assistant messages from the
finalcaching candidates when the provider is@ai-sdk/github-copilot. Only user and tool messages receivecopilot_cache_controloptions. This is a minimal, provider-scoped change that does not affect any other provider.Confidence caveat
If the maintainers have gateway source access and can confirm or refute the causal chain, that would be valuable.