Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with LangChain ChatPromptTemplate.from_messages

As shown in LangChain Quickstart, I am trying the following Python code:

from langchain.prompts.chat import ChatPromptTemplate
template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])

chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")

But when I run the above code, I get the following error:

Traceback (most recent call last):
   File "/home/yser364/Projets/SinappsIrdOpenaiQA/promptWorkout.py", line 6, in <module>
     chat_prompt = ChatPromptTemplate.from_messages([
   File "/home/yser364/.local/lib/python3.10/site-packages/langchain/prompts/chat.py", line 220, in from_messages
     return cls(input_variables=list(input_vars), messages=messages)
   File "/home/yser364/.local/lib/python3.10/site-packages/langchain/load/serializable.py", line 64, in __init__
     super().__init__(**kwargs)
   File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
 pydantic.error_wrappers.ValidationError: 4 validation errors for ChatPromptTemplate
 messages -> 0
   value is not a valid dict (type=type_error.dict)
 messages -> 0
   value is not a valid dict (type=type_error.dict)
 messages -> 1
   value is not a valid dict (type=type_error.dict)
 messages -> 1
   value is not a valid dict (type=type_error.dict)

I use Python 3.10.12.

like image 272
Yannick Serra Avatar asked Oct 27 '25 09:10

Yannick Serra


1 Answers

Your example is from the Prompt templates section of the LangChain Quickstart tutorial. I did not spot any differences, so it should work as given.

I tried out the example myself, with an additional loop to output the messages created by chat_prompt.format_messages:

from langchain.prompts.chat import ChatPromptTemplate
template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])

messages = chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
for message in messages:
    print(message.__repr__())

The example works without any errors. The result is very similar to what is shown in the tutorial, although not identical:

SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={})
HumanMessage(content='I love programming.', additional_kwargs={}, example=False)

I ran the test with Python 3.9.5 and LangChain 0.0.300, which is the lastest version on PyPI. According to PyPI, it supports Python >=3.8.1 and <4.0.

Maybe your version of LangChain or one of its dependencies is outdated? Try to run it in a new venv with a fresh install of LangChain.

like image 89
hbhbnr Avatar answered Oct 29 '25 00:10

hbhbnr