How to Fix 'prompt template error' in LangChain (TypeScript)

By Cyprian AaronsUpdated 2026-04-22
prompt-template-errorlangchaintypescript

What this error means

prompt template error in LangChain usually means the prompt renderer could not resolve one or more variables in your template. In TypeScript, it typically shows up when you pass the wrong variable names, forget to supply inputVariables, or mix template syntax with the wrong prompt class.

The stack trace often points at PromptTemplate, ChatPromptTemplate, or formatMessages(). The real issue is almost always a mismatch between what the template expects and what your code passes in.

The Most Common Cause

The #1 cause is a variable name mismatch between the template and the values you pass at runtime.

Here’s the broken pattern:

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = PromptTemplate.fromTemplate(
  "Write a short summary of {text}"
);

const result = await prompt.format({
  content: "LangChain is a framework for building LLM apps.",
});

This throws an error like:

Error: Missing value for input variable `text`

Or, depending on the path:

Error: prompt template error: Missing value for input variable(s): text

The fix is to pass the exact variable name used in the template:

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = PromptTemplate.fromTemplate(
  "Write a short summary of {text}"
);

const result = await prompt.format({
  text: "LangChain is a framework for building LLM apps.",
});

If you prefer explicit typing, define the variables up front:

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = new PromptTemplate({
  template: "Write a short summary of {text}",
  inputVariables: ["text"],
});

const result = await prompt.format({
  text: "LangChain is a framework for building LLM apps.",
});

Broken vs fixed

BrokenFixed
template: "Hello {name}"<br>format({ userName: "Sam" })template: "Hello {name}"<br>format({ name: "Sam" })
inputVariables: ["query"]<br>format({ question: "Hi" })inputVariables: ["query"]<br>format({ query: "Hi" })

Other Possible Causes

1. You used curly braces in plain text without escaping them

LangChain treats {} as template variables. If you want literal braces, escape them.

// Broken
const prompt = PromptTemplate.fromTemplate(
  "Return JSON like {\"status\": \"ok\"}"
);

This can trigger parsing errors because LangChain thinks "status" is a variable.

Fix it by escaping braces:

// Fixed
const prompt = PromptTemplate.fromTemplate(
  'Return JSON like {{ "status": "ok" }}'
);

2. You passed an array of messages to the wrong prompt class

PromptTemplate expects a string template. If you're building chat prompts, use ChatPromptTemplate.

// Broken
import { PromptTemplate } from "@langchain/core/prompts";

const prompt = new PromptTemplate({
  template: [
    ["system", "You are helpful"],
    ["human", "{question}"],
  ] as any,
});

Use the chat-specific class instead:

// Fixed
import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are helpful"],
  ["human", "{question}"],
]);

3. Your partial variables collide with runtime variables

If you set a partial variable and then reuse the same name, LangChain can fail during formatting.

// Broken
const prompt = PromptTemplate.fromTemplate("Hello {name}, today is {day}");

const partial = await prompt.partial({
  name: "Sam",
});

await partial.format({
  name: "Alex",
  day: "Monday",
});

Avoid reusing the same key. Keep partials and runtime inputs distinct.

// Fixed
const prompt = PromptTemplate.fromTemplate("Hello {name}, today is {day}");

const partial = await prompt.partial({
  day: "Monday",
});

await partial.format({
  name: "Alex",
});

4. You have an extra variable in inputVariables that never appears in the template

This often fails validation before execution.

// Broken
const prompt = new PromptTemplate({
  template: "Tell me about {topic}",
  inputVariables: ["topic", "tone"],
});

If tone is not used, remove it or include it in the template.

// Fixed
const prompt = new PromptTemplate({
  template: "Tell me about {topic} in a {tone} tone",
  inputVariables: ["topic", "tone"],
});

How to Debug It

  1. Read the exact missing variable name

    • The error message usually tells you which key is missing.
    • Look for messages like:
      • Missing value for input variable(s): text
      • InvalidPromptInputError
      • prompt template error
  2. Print the resolved template before calling the model

    • Validate what LangChain thinks your inputs are.
    • Example:
console.log(prompt.inputVariables);
console.log(await prompt.format({ text: "..."}));
  1. Check whether you're using string prompts or chat prompts

    • Use PromptTemplate for plain text.
    • Use ChatPromptTemplate for message arrays.
    • If you see { role, content } or [["system", "..."]], you're probably in chat land.
  2. Search for unescaped braces

    • JSON examples, code snippets, and object literals inside templates are common offenders.
    • Replace literal { and } with doubled braces:
      • {{{
      • }}}

Prevention

  • Keep template variables explicit and typed:
    const vars = ["question", "context"] as const;
    
  • Add unit tests that call .format() or .formatMessages() with sample inputs.
  • Standardize on one pattern per project:
    • PromptTemplate for single-string prompts
    • ChatPromptTemplate for multi-message prompts

Keep learning

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

Related Guides