Back to All questionsHow do I fix KeyError: "Input to ChatPromptTemplate is missing variables {'chat_history'}"?
Ask questionpython
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
prompt = ChatPromptTemplate.from_messages([
("system", "You are helpful"),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}")
])
formatted = prompt.invoke({"input": "Hi"})
Error:
text
KeyError: "Input to ChatPromptTemplate is missing variables {'chat_history'}. Expected:
['agent_scratchpad', 'chat_history', 'input'] Received: ['input']"
1 answerHelpful solutions from the community
Fix:
python
from langchain_core.messages import HumanMessage
formatted = prompt.invoke({
"input": "Hi",
"chat_history": [HumanMessage(content="Previous message")]
})
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.