[Python] Error with LangChain ChatPromptTemplate.from_messages

Discussão em 'Python' iniciado por Stack, Junho 1, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página