Repository navigation
fix(anthropic): end requests on a user turn (Fixes #7861) - #7862
Open
ninglinLiu wants to merge 14 commits into
Open
ninglinLiu wants to merge 14 commits into
ninglinLiu wants to merge 14 commits into
Conversation
Claude treats a trailing assistant message as a prefill. Claude 4.6+ rejects that; older models continue the previous reply. Append the same synthetic user turn BaseLlm._maybe_append_user_content uses, after the empty-message filter and after cache breakpoints so the synthetic turn is not part of the cached session prefix. Fixes google#7861
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.
Related issue
Fixes #7861
Summary
AnthropicLlm.generate_content_asyncconvertedllm_request.contentswithout ensuring the Anthropic messages end on a user turn. ALoopAgent(or any re-run on the agent's last reply) can therefore send[user, assistant]. Claude treats a final assistant message as a prefill: Claude 4.6+ returns 400, and older models continue the previous reply instead of answering again.Gemini / LiteLlm / Apigee already call
BaseLlm._maybe_append_user_content. This change appends the same synthetic user text after empty-message filtering and after cache breakpoints, so a dropped turn cannot leave two user turns in a row and the synthetic turn is not part of the cached session prefix.Verification
pytest tests/unittests/models/test_anthropic_llm.py -k 'appends_user_when_history_ends_on_assistant or keeps_single_user_when_already_trailing' -qpassesAI assistance
Assisted by an AI coding agent; the fix and tests were reviewed and verified locally before opening this PR.