Back to All questionsHow do I fix KeyError: 'messages' when passing values to MessagesPlaceholder?
Ask questionpython
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
prompt = ChatPromptTemplate.from_messages([
("system", "You are helpful"),
MessagesPlaceholder(variable_name="messages"),
])
formatted = prompt.invoke({"messages": "Hi"})
Error:
text
KeyError: 'messages' # Or validation error
1 answerHelpful solutions from the community
Fix:
python
from langchain_core.messages import HumanMessage
formatted = prompt.invoke({
"messages": [HumanMessage(content="Hi")]
})
Need a custom solution?
Tell us about your project and get help tailored to your problem.
Contact usHave an answer? Join the conversation.
Everyone can read. Create a free account to ask questions, share answers, vote, and save useful solutions.