fix(anthropic): strip trailing assistant messages for Claude 4.6+#4973
Open
giulio-leone wants to merge 1 commit intolivekit:mainfrom
Open
fix(anthropic): strip trailing assistant messages for Claude 4.6+#4973giulio-leone wants to merge 1 commit intolivekit:mainfrom
giulio-leone wants to merge 1 commit intolivekit:mainfrom
Conversation
b5ad333 to
88c3b35
Compare
Claude 4.6 models no longer support prefilling (trailing assistant messages in the request). Requests ending with an assistant turn now return a 400 error. Add a `strip_trailing_assistant` parameter to the Anthropic provider format converter and enable it automatically for Claude 4.6+ models (claude-sonnet-4-6, claude-opus-4-6). A `len(messages) > 1` guard ensures at least one message is always preserved. Fixes livekit#4907
88c3b35 to
0b3dc1c
Compare
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.
Summary
Claude 4.6 models (
claude-sonnet-4-6,claude-opus-4-6) no longer support assistant message prefilling. Requests where the last message hasrole="assistant"now return a 400 Bad Request error, breaking existing agent workflows.Root Cause
In multi-turn conversations, the chat context can naturally end with an assistant message (the last thing the agent said). When this context is sent to Claude 4.6 via
messages.create(), the Anthropic API interprets the trailing assistant turn as a prefill attempt and rejects it.Fix
Two surgical changes:
livekit-agents/.../anthropic.py(provider format converter): Added astrip_trailing_assistantparameter toto_chat_ctx(). When enabled, trailing assistant messages are stripped from the request so it ends with a user turn. Alen(messages) > 1guard ensures at least one message is always preserved.livekit-plugins/.../anthropic/llm.py(Anthropic LLM plugin): Added_model_disables_prefill()to detect Claude 4.6+ models by name prefix. Theto_provider_format()call now passesstrip_trailing_assistant=Trueonly for affected models.This approach:
startswith()matching for forward compatibility with future 4.6 variantsstrip_trailing_assistant=False)Testing
The fix is minimal and backward-compatible. Existing behavior is unchanged for all non-4.6 models.
Fixes #4907