Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws[patch]: fix ordering of tool result content blocks #389

Merged
merged 4 commits into from
Mar 6, 2025

Conversation

ccurme
Copy link
Contributor

@ccurme ccurme commented Mar 4, 2025

langchain-ai/langgraph#2161

Anthropic requires that tool result content blocks precede text in user messages. The functionality to merge messages does not account for this.

from langchain_aws import ChatBedrock
from langchain_core.messages import (
    AIMessage,
    HumanMessage,
    SystemMessage,
    ToolMessage,
)
from langchain_core.tools import tool


@tool
def get_weather(location: str) -> str:
    """Get weather at a location."""
    return "It's sunny."

system = SystemMessage("You are a helpful assistant.")
human = HumanMessage("What is the weather in Boston?")
ai = AIMessage(
    "",
    tool_calls=[
        {"name": "get_weather", "id": "1", "args": {"location": "Boston, MA"}},
    ],
)
tool = ToolMessage(
    "It's sunny.",
    tool_call_id="1",
)
human_2 = HumanMessage("That doesn't look right.")
messages = [system, human, ai, tool, human_2]


llm = ChatBedrock(
    model="anthropic.claude-3-5-sonnet-20240620-v1:0",
).bind_tools([get_weather])

response = llm.invoke(messages)

^ Before this PR, this will raise:

ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: messages.2: Did not find 1 tool_result block(s) at the beginning of this message. Messages following tool_use blocks must begin with a matching number of tool_result blocks.

cc @laithalsaadoon @andresriancho

Copy link
Collaborator

@michaelnchin michaelnchin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @ccurme

@michaelnchin michaelnchin merged commit 662c995 into main Mar 6, 2025
12 checks passed
@michaelnchin michaelnchin deleted the cc/anthropic_merge_messages_bug branch March 6, 2025 18:49
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.

2 participants