Skip to content

Conversation

michaelnchin
Copy link
Collaborator

Addresses #498 for ChatBedrock.

@michaelnchin michaelnchin linked an issue Aug 7, 2025 that may be closed by this pull request
Copy link
Collaborator

@3coins 3coins left a comment

Choose a reason for hiding this comment

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

@michaelnchin
Thanks for submitting this change.

After looking at the langchain-anthropic integration, I am not convinced if it allows system messages to be present anywhere in the message sequence. While the logic to allow merging of consecutive system messages at the beginning seems valid, the actual change here seems to extract system messages that might be non-consecutive, which will conflict with anthropic's expectations.
Can you request the issue reporter to provide a small sample case to test against and make sure we can validate that with anthropic's native integration?

@michaelnchin
Copy link
Collaborator Author

One note, this PR's changes mirrors those made a previous PR for langchain-anthropic: langchain-ai/langchain#27725

In both implementations, non-consecutive system messages are indeed initially extracted by _merge_messages, but will then be later caught by this check.

I've added an extra test case to verify that we properly throw an error in such cases.

@00LAVA00
Copy link

Hi! I was facing the same issue described in #498 and can confirm that @michaelnchin changes work.

Here's a minimal example that was failing before but works with these changes:

from langchain_aws import ChatBedrock
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.messages.utils import count_tokens_approximately
from langgraph.prebuilt import create_react_agent
from langmem.short_term import SummarizationNode
from langgraph.checkpoint.memory import InMemorySaver

llm = ChatBedrock(model_id="us.anthropic.claude-3-5-sonnet-20241022-v2:0")

# library code: langmem.short_term.summarization_node
# this is what was causing the error before the fix
final_prompt = ChatPromptTemplate.from_messages(
    [
        # if exists
        ("placeholder", "{system_message}"),
        ("system", "Summary of the conversation so far: {summary}"),
        ("placeholder", "{messages}"),
    ]
)

summarization_node = SummarizationNode(
    token_counter=count_tokens_approximately,
    model=llm,
    max_tokens=150,
    max_tokens_before_summary=70,
    max_summary_tokens=30,
    final_prompt=final_prompt,
    output_messages_key="llm_input_messages",
)

checkpointer = InMemorySaver()
agent = create_react_agent(
    model=llm,
    tools=[],
    pre_model_hook=summarization_node,
    prompt=SystemMessage(content="You are a helpful data assistant."),
    checkpointer=checkpointer,
)


async def run():
    config = {"configurable": {"thread_id": "test"}}    
    async for event in agent.astream_events(
        {"messages": [HumanMessage(content="test, tell me about paris - 50 words")]},
        config=config,
        version="v2",
    ):
        if event.get("event") == "on_chat_model_stream":
            chunk = event["data"]["chunk"]
            print(chunk.content if hasattr(chunk, 'content') else chunk)


    async for event in agent.astream_events(
        {"messages": [HumanMessage(content="Hello, tell me about campaign abc in 20 words")]},
        config=config,
        version="v2",
    ):
        if event.get("event") == "on_chat_model_stream":
            chunk = event["data"]["chunk"]
            print(chunk.content if hasattr(chunk, 'content') else chunk)

When summarization triggers, it creates multiple system messages that violate the "system message must be at beginning" validation.

Happy to provide more details if needed!

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.

"System message must be at beginning of message list" limitation
3 participants