Skip to content

fix(ai-gateway): append placeholder user message for trailing assistant message on Anthropic chat_completions#2530

Closed
kilo-code-bot[bot] wants to merge 1 commit intomainfrom
fix/anthropic-chat-completions-trailing-assistant-message
Closed

fix(ai-gateway): append placeholder user message for trailing assistant message on Anthropic chat_completions#2530
kilo-code-bot[bot] wants to merge 1 commit intomainfrom
fix/anthropic-chat-completions-trailing-assistant-message

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 16, 2026

Summary

  • When a client sends a chat_completions request to an Anthropic model whose final message is an assistant message, append a minimal placeholder user message (Continue.) so the upstream provider accepts the request.
  • Scoped to Anthropic models via chat_completions only — the native Messages API (which supports assistant prefills) and the Responses API are untouched.
  • Implemented inside applyAnthropicModelSettings next to the existing chat_completions-only tool-call-id normalization.

Verification

  • pnpm --filter web typecheck
  • pnpm format
  • pnpm --filter web lint
  • Added apps/web/src/lib/ai-gateway/providers/anthropic.test.ts covering the new function and the three request kinds; tests could not be executed locally because Docker/Postgres are unavailable in this environment, so CI is the primary verification.

Visual Changes

N/A

Reviewer Notes

  • Placeholder content is the literal string "Continue." — adjust if a different wording is preferred.
  • This only runs when the request reaches applyAnthropicModelSettings, which is gated by isAnthropicModel(requestedModel).

…ompletions when last message is from assistant
// anthropic doesn't allow '.' in tool call ids
// we can fix this later for the responses api if it's still a problem
normalizeToolCallIds(requestToMutate.body, toolCallId => toolCallId.includes('.'), undefined);
appendPlaceholderUserMessageIfLastIsAssistant(requestToMutate.body);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: This rewrites valid assistant-prefill requests on providers that already accept them

applyAnthropicModelSettings runs for every Anthropic model before we know which upstream provider will handle the request. We already prefer Bedrock/Anthropic specifically because Vertex is the provider that rejects assistant prefills, so unconditionally turning a trailing assistant message into a new user turn here changes the semantics of requests that would otherwise be valid. Prefix-steering cases like JSON/code prefills will now be treated as a follow-up user prompt instead of an assistant prefill.

Copy link
Copy Markdown
Contributor Author

@kilo-code-bot kilo-code-bot bot Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional. The OpenAI chat_completions wire format has no native concept of an assistant prefill — the last message being assistant is just a normal conversation turn that expects a follow-up user message. This workaround is specifically for chat_completions clients that accidentally leave a trailing assistant message (which Anthropic's Messages API would reject when the request is translated). Clients that want true Anthropic assistant prefill semantics should use the native Messages API path, which this code leaves untouched.

@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 16, 2026

Code Review Summary

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
apps/web/src/lib/ai-gateway/providers/anthropic.ts 52 Unconditionally appending a placeholder user turn changes valid Anthropic assistant-prefill requests before provider selection, so requests routed to Bedrock/Anthropic can change behavior instead of just avoiding the Vertex-specific 400.
Other Observations (not in diff)

No additional issues found outside the diff.

Files Reviewed (2 files)
  • apps/web/src/lib/ai-gateway/providers/anthropic.ts - 1 issue
  • apps/web/src/lib/ai-gateway/providers/anthropic.test.ts - 0 issues

Reviewed by gpt-5.4-20260305 · 458,825 tokens

@chrarnoldus chrarnoldus marked this pull request as draft April 17, 2026 12:14
@chrarnoldus chrarnoldus deleted the fix/anthropic-chat-completions-trailing-assistant-message branch April 20, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant