fix(vertexai): allow system messages at any position in ChatAnthropicVertex#1610
Merged
Leonid Kuligin (lkuligin) merged 1 commit intolangchain-ai:mainfrom Feb 24, 2026
Conversation
…Vertex The _format_messages_anthropic() function required system messages to be at index 0, raising ValueError otherwise. This prevented valid patterns like [HumanMessage, SystemMessage, ...] that are supported by the Anthropic API and by langchain-anthropic. This change removes the position check and instead validates that non-consecutive system messages are not present (raising a clear error). Consecutive system messages are merged. The _merge_messages() function is extended to merge consecutive SystemMessages the same way it already merged consecutive HumanMessages. Fixes langchain-ai#1022
Leonid Kuligin (lkuligin)
approved these changes
Feb 24, 2026
7453adb
into
langchain-ai:main
16 checks passed
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.
Problem
The
_format_messages_anthropic()function required system messages to be at index 0, raisingValueError("System message must be at beginning of message list.")otherwise. This prevented valid patterns like[HumanMessage(...), SystemMessage(...), ...]that are supported by the Anthropic API and bylangchain-anthropic.Error from issue #1022:
Solution
if i != 0) in_format_messages_anthropic()ValueError)_merge_messages()to merge consecutiveSystemMessages the same way it already merged consecutiveHumanMessagesmodel_copy(update=...)instead of mutating message content in-placeTests
ValueErrorChanges
libs/vertexai/langchain_google_vertexai/_anthropic_utils.pylibs/vertexai/tests/unit_tests/test_anthropic_utils.pyFixes #1022