Skip to content

fix(transform): skip trailing assistant messages in applyCaching for GitHub Copilot#29609

Closed
FredrikFolkeryd wants to merge 1 commit into
anomalyco:devfrom
FredrikFolkeryd:fix/copilot-prefill-on-trailing-assistant-message
Closed

fix(transform): skip trailing assistant messages in applyCaching for GitHub Copilot#29609
FredrikFolkeryd wants to merge 1 commit into
anomalyco:devfrom
FredrikFolkeryd:fix/copilot-prefill-on-trailing-assistant-message

Conversation

@FredrikFolkeryd
Copy link
Copy Markdown

Problem

When using Claude models via the GitHub Copilot gateway (e.g. github-copilot/claude-sonnet-4.6), applyCaching stamps copilot_cache_control providerOptions 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:

400 Bad Request: This model does not support assistant message prefill.
The conversation must end with a user message.

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 final caching candidates when the provider is @ai-sdk/github-copilot. Only user and tool messages receive copilot_cache_control options. This is a minimal, provider-scoped change that does not affect any other provider.

if (model.api.npm === "@ai-sdk/github-copilot") {
  final = final.filter((msg) => msg.role !== "assistant")
}

Confidence caveat

Note: The root cause (providerOptions → Copilot gateway prefill translation → Claude 400) was identified through AI-assisted static analysis of transform.ts and the @ai-sdk/github-copilot SDK behaviour. The exact mechanism by which copilot_cache_control on an assistant message causes the gateway to emit a trailing prefill block has not been independently verified against the Copilot gateway source. The fix is conservative and the commit message makes this explicit.

If the maintainers have gateway source access and can confirm or refute the causal chain, that would be valuable.

…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.
@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels May 27, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions
Copy link
Copy Markdown
Contributor

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.

@github-actions
Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label May 27, 2026
@github-actions github-actions Bot closed this May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant