How to Fix 'prompt template error during development' in LangChain (TypeScript)
If you’re seeing Error: prompt template error during development, LangChain is failing while building or validating your prompt template before the chain runs. In TypeScript, this usually shows up when variables in the template do not match the variables you passed in, or when a prompt is created with the wrong API shape.
This error typically appears during local development, especially after refactors, when you rename placeholders, switch from string prompts to chat prompts, or pass dynamic values into PromptTemplate, ChatPromptTemplate, or RunnableSequence.
The Most Common Cause
The #1 cause is a mismatch between template variables and the values you provide at runtime.
LangChain validates prompt inputs early. If your template expects {question} but you pass {input}, you’ll get a failure like:
- •
Missing value for input variable 'question' - •
Input to ChatPromptTemplate is missing variables - •
PromptTemplate.fromTemplate(...)validation error
Here’s the broken pattern and the fixed pattern side by side.
| Broken | Fixed |
|---|---|
| ```ts | |
| import { PromptTemplate } from "@langchain/core/prompts"; |
const prompt = PromptTemplate.fromTemplate( "Answer this: {question}" );
const result = await prompt.format({
input: "What is LangChain?"
});
|ts
import { PromptTemplate } from "@langchain/core/prompts";
const prompt = PromptTemplate.fromTemplate( "Answer this: {question}" );
const result = await prompt.format({ question: "What is LangChain?" });
The same problem happens with chat prompts:
| Broken | Fixed |
|---|---|
| ```ts
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant."],
["human", "{input}"]
]);
await prompt.formatMessages({
question: "Explain retries"
});
``` | ```ts
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant."],
["human", "{input}"]
]);
await prompt.formatMessages({
input: "Explain retries"
});
``` |
If you use `RunnableSequence`, the mismatch is often hidden one layer deeper. The chain compiles, then fails when the prompt tries to render.
```ts
import { RunnableSequence } from "@langchain/core/runnables";
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
["human", "Summarize this: {text}"]
]);
const chain = RunnableSequence.from([
{
text: (input: { content: string }) => input.content,
},
prompt,
]);
await chain.invoke({ content: "Hello world" });
Fix it by making sure the upstream key matches the template variable exactly:
const chain = RunnableSequence.from([
{
text: (input: { content: string }) => input.content,
},
prompt,
]);
await chain.invoke({ content: "Hello world" });
In practice, if your template says {text}, some step in the pipeline must produce text.
Other Possible Causes
1. You used braces that were meant as literal text
LangChain treats {} as placeholders. If you paste JSON or examples into a template without escaping braces, it will try to parse them as variables.
// Broken
const prompt = PromptTemplate.fromTemplate(
'Return JSON like {"name": "Alice"}'
);
Fix by escaping literal braces:
// Fixed
const prompt = PromptTemplate.fromTemplate(
'Return JSON like {{ "name": "Alice" }}'
);
2. You passed a non-string where LangChain expected a string
This comes up when composing prompts with objects, arrays, or undefined values.
// Broken
await prompt.format({
question: undefined
});
Make sure values are normalized before formatting:
// Fixed
await prompt.format({
question: String(question ?? "")
});
3. Your inputVariables declaration does not match the template
This happens more often when using new PromptTemplate(...) instead of fromTemplate(...).
// Broken
const prompt = new PromptTemplate({
template: "Tell me about {topic}",
inputVariables: ["subject"],
});
Correct it so the declared variables match the placeholders:
// Fixed
const prompt = new PromptTemplate({
template: "Tell me about {topic}",
inputVariables: ["topic"],
});
4. You mixed chat message placeholders with plain string templates
ChatPromptTemplate and PromptTemplate are not interchangeable. A chat template expects message structure, while a string template expects one formatted string.
// Broken
const prompt = new PromptTemplate({
template: [
["system", "You are helpful"],
["human", "{input}"],
] as any,
inputVariables: ["input"],
});
Use the right class:
// Fixed
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are helpful"],
["human", "{input}"],
]);
How to Debug It
- •
Inspect the exact placeholder names
- •Search for
{...}in your template. - •Compare them to the object passed into
.format(),.formatMessages(), or.invoke().
- •Search for
- •
Print the resolved inputs before formatting
- •Log the final object right before it reaches LangChain.
- •Look for
undefined, renamed keys, or nested objects that never get flattened.
- •
Check which class you’re using
- •
PromptTemplateexpects a string template. - •
ChatPromptTemplateexpects messages. - •If you switched one for the other during refactoring, that’s usually the bug.
- •
- •
Reduce to a minimal repro
- •Strip out retrievers, tools, memory, and model calls.
- •Keep only the prompt construction and one
.format()call. - •If it still fails, the issue is in the template itself.
Prevention
- •Keep placeholder names consistent across your entire chain.
- •Add TypeScript types for your chain inputs so missing keys fail at compile time.
- •Validate templates in tests with one happy-path render before shipping changes.
- •Escape literal braces any time you include JSON examples or code snippets inside prompts.
If you want this class of bug to disappear from your codebase, treat prompts like typed interfaces. The moment your schema and your runtime payload drift apart, LangChain will tell you immediately — usually through this exact error.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit