langchain-aws: Converse operation: messages.2.content: Conversation blocks and tool result blocks cannot be provided in the same turn.

With the ChatBedrock class, the following code works as expected:

from langchain_core.prompts import ChatPromptTemplate
from langchain_aws import ChatBedrock
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_core.messages import AIMessage, ToolMessage


def rag_query(query: str) -> str:
    """Do a RAG search in a internal knowledge base. """
    return "No results found !!!"

def web_query(query: str) -> str:
    """Search the web for the query."""
    return "Connection error"

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant with tools."),
    ("human", "Tell me who was Mozart ?"),
    AIMessage("", tool_calls=[{"name": "rag_query", "args": {"query": "Who was Mozart"}, "id": 1}]),
    ToolMessage("You correctly called the tool", tool_call_id="1"),
    ("human", "Who was Bethoveen ?"),
    AIMessage("", tool_calls=[{"name": "rag_query", "args": {"query": "Who was Bethoveen"}, "id": 2}]),
    ToolMessage("You correctly called the tool", tool_call_id="2"),
    ("human", "How to install linux ?"),
    AIMessage("", tool_calls=[{"name": "web_query", "args": {"query": "Tutorial on how to install Linux"}, "id": 3}]),
    ToolMessage("You correctly called the tool", tool_call_id="3"),
    ("human", "What is nmap"),
    AIMessage("", tool_calls=[{"name": "web_query", "args": {"query": "nmap documentation"}, "id": 4}]),
    ToolMessage("You correctly called the tool", tool_call_id="4"),
    ("human", "{question}")
])

bedrock = boto3.client('bedrock-runtime')
llm = ChatBedrock(model_id="anthropic.claude-3-haiku-20240307-v1:0", client=bedrock)

chain = {"question": RunnablePassthrough()}|  prompt | llm.bind_tools([rag_query, web_query])
chain.invoke("How to use docker ?")

Result:

AIMessage(content='', additional_kwargs={'usage': {'prompt_tokens': 741, 'completion_tokens': 54, 'total_tokens': 795}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-haiku-20240307-v1:0'}, response_metadata={'usage': {'prompt_tokens': 741, 'completion_tokens': 54, 'total_tokens': 795}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-haiku-20240307-v1:0'}, id='run-f7e8b75a-ceeb-4021-952d-39a74c880203-0', tool_calls=[{'name': 'web_query', 'args': {'query': 'docker tutorial'}, 'id': 'toolu_bdrk_01KGdyLm9JyGVVZQ4VQF1ZPR', 'type': 'tool_call'}], usage_metadata={'input_tokens': 741, 'output_tokens': 54, 'total_tokens': 795})

But when using the ChatBedrockConverse class:

from langchain_core.prompts import ChatPromptTemplate
from langchain_aws import ChatBedrockConverse
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_core.messages import AIMessage, ToolMessage


def rag_query(query: str) -> str:
    """Do a RAG search in a internal knowledge base. """
    return "No results found !!!"

def web_query(query: str) -> str:
    """Search the web for the query."""
    return "Connection error"

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant with tools."),
    ("human", "Tell me who was Mozart ?"),
    AIMessage("", tool_calls=[{"name": "rag_query", "args": {"query": "Who was Mozart"}, "id": 1}]),
    ToolMessage("You correctly called the tool", tool_call_id="1"),
    ("human", "Who was Bethoveen ?"),
    AIMessage("", tool_calls=[{"name": "rag_query", "args": {"query": "Who was Bethoveen"}, "id": 2}]),
    ToolMessage("You correctly called the tool", tool_call_id="2"),
    ("human", "How to install linux ?"),
    AIMessage("", tool_calls=[{"name": "web_query", "args": {"query": "Tutorial on how to install Linux"}, "id": 3}]),
    ToolMessage("You correctly called the tool", tool_call_id="3"),
    ("human", "What is nmap"),
    AIMessage("", tool_calls=[{"name": "web_query", "args": {"query": "nmap documentation"}, "id": 4}]),
    ToolMessage("You correctly called the tool", tool_call_id="4"),
    ("human", "{question}")
])

bedrock = boto3.client('bedrock-runtime')
llm = ChatBedrockConverse(model_id="anthropic.claude-3-haiku-20240307-v1:0", client=bedrock)

chain = {"question": RunnablePassthrough()}|  prompt | llm.bind_tools([rag_query, web_query])
chain.invoke("How to use docker ?")

I receive the following exception:

ValidationException: An error occurred (ValidationException) when calling the Converse operation: messages.2.content: Conversation blocks and tool result blocks cannot be provided in the same turn.

@baskaryan

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 8
  • Comments: 18 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Any updates over here?

I am experiencing this too with the Converse API. It doesn’t matter if you remove adjacent human and tool messages. For me as soon as I add memory i.e a list of messages. I start getting the error.

Everything only works if you are doing single-turn runs