Missing some input keys / Input to PromptTemplate is missing variables (LangChain)
The variables in your prompt template do not match the keys you passed at run time — often because literal JSON braces in the template read as variables. Escape braces as {{ }} and align the key names.
Updated
The error
KeyError: "Input to PromptTemplate is missing variables {'context'}. Expected: ['context', 'question'] Received: ['question']"Older LangChain versions word it as:
ValueError: Missing some input keys: {'context'}What it means
A prompt template is a string with named slots — {context}, {question} — filled at run time. LangChain compares the slots the template declares against the keys you supplied. The sets differ: the template wants context and question; you sent only question. The message lists both sides, which makes the mismatch readable once you know to compare them.
Why it happens
Three routes:
- A plain mismatch. The template says
{context}, the code passesdocsorContext. Template variables are case-sensitive exact matches. - Unescaped braces. Any
{...}in the template counts as a variable. A JSON example inside your prompt —{"name": "..."}— silently declares a variable named"name", and suddenly the template "expects" keys you never intended. - Chain wiring. In multi-step chains, an upstream step outputs a key (say
text) that the downstream template does not use, while the template's key is fed by nothing.
How to fix it
1. Compare the two lists in the message. They are the whole diagnosis. Rename one side so they match:
from langchain_core.prompts import PromptTemplate
prompt = PromptTemplate.from_template(
"Answer using the context.\n\nContext: {context}\n\nQuestion: {question}"
)
prompt.invoke({"context": docs_text, "question": user_q})2. Escape literal braces by doubling them. For JSON examples inside prompts:
prompt = PromptTemplate.from_template(
'Return JSON like {{"city": "...", "score": 0}}.\n\nText: {text}'
){{ and }} render as single braces and declare no variable. Check what the template actually inferred:
print(prompt.input_variables)['text']
3. Pre-fill constants with partials. For values known ahead of time:
prompt = prompt.partial(today="2026-08-31")The remaining call then supplies only the truly dynamic keys.
4. In chains, print what flows between steps. Log the dict a step emits and the input_variables of the next template; the missing key is visible at the seam. Renaming with a small mapping step (RunnablePassthrough.assign or an itemgetter) closes the gap.
How to prevent it
After writing any template, print input_variables once — it catches both typos and brace leaks immediately. Keep variable names identical across template, chain wiring and calling code rather than translating at each layer.
Related errors
- ImportError: cannot import name 'OpenAI' from 'langchain'
- No module named 'langchain_community'
- JSONDecodeError: Expecting value — the output-side counterpart of prompt problems