Mastering PromptTemplate in LangChain.js
Aug 7,2026

In the previous article, we learned how to interact with AI models using invoke(), batch(), and stream().

Most of our examples looked like this:

const response = await model.invoke(
  "Explain Node.js."
);

This works well for simple examples.

But imagine you're building a real application where users can ask about different topics.

Should you write a new prompt every time?

await model.invoke("Explain React.");

await model.invoke("Explain Node.js.");

await model.invoke("Explain Express.js.");

await model.invoke("Explain MongoDB.");

This approach quickly becomes repetitive and difficult to maintain.

LangChain solves this problem with PromptTemplate.

A PromptTemplate allows you to create reusable prompts with placeholders that can be filled dynamically at runtime.

In this article, you'll learn:

  • What PromptTemplate is
  • Why it's useful
  • Variables
  • Multiple Variables
  • Partial Variables
  • format()
  • pipe()
  • OpenAI examples
  • Gemini examples

Let's get started.


What is PromptTemplate?

A PromptTemplate is a reusable prompt that contains placeholders for dynamic values.

Instead of writing:

Explain Node.js in simple words.

You can write:

Explain {topic} in simple words.

Later, you provide a value for topic.

This makes your prompts reusable and easier to maintain.


Why Do We Need PromptTemplate?

Without PromptTemplate:

await model.invoke("Explain React.");

await model.invoke("Explain Node.js.");

await model.invoke("Explain Express.js.");

You repeat almost the same prompt with only the topic changing.

With PromptTemplate:

Explain {topic} in simple words.

You define the prompt once and reuse it with different values.


Project Setup

npm install langchain @langchain/openai @langchain/google-genai dotenv

Import Required Modules

import "dotenv/config";

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

import { ChatOpenAI } from "@langchain/openai";

import { ChatGoogleGenerativeAI } from "@langchain/google-genai";

Initialize OpenAI

const openAIModel = new ChatOpenAI({
  model: "gpt-4.1-mini",
  temperature: 0.7,
});

Initialize Gemini

const geminiModel =
  new ChatGoogleGenerativeAI({
    model: "gemini-2.5-flash",
    temperature: 0.7,
  });

Creating Your First PromptTemplate

const prompt =
  PromptTemplate.fromTemplate(
    "Explain {topic} in simple words."
  );

The {topic} placeholder will be replaced with a real value later.


Formatting a Prompt

Use the format() method to replace variables.

const formattedPrompt =
  await prompt.format({
    topic: "Node.js",
  });

console.log(formattedPrompt);

Output:

Explain Node.js in simple words.

Using PromptTemplate with OpenAI

const prompt =
  PromptTemplate.fromTemplate(
    "Explain {topic} in simple words."
  );

const formattedPrompt =
  await prompt.format({
    topic: "React",
  });

const response =
  await openAIModel.invoke(
    formattedPrompt
  );

console.log(response.content);

Using PromptTemplate with Gemini

const formattedPrompt =
  await prompt.format({
    topic: "Express.js",
  });

const response =
  await geminiModel.invoke(
    formattedPrompt
  );

console.log(response.content);

The only thing that changes is the model.


Multiple Variables

A prompt often needs more than one input.

const prompt =
  PromptTemplate.fromTemplate(
    "Explain {topic} for a {level} developer."
  );

Format the prompt:

const formattedPrompt =
  await prompt.format({
    topic: "Node.js",
    level: "Beginner",
  });

Generated prompt:

Explain Node.js for a Beginner developer.

Partial Variables

Sometimes part of a prompt never changes.

For example, your AI always responds in English.

Instead of passing language every time:

const prompt =
  PromptTemplate.fromTemplate(
    "Explain {topic} in {language}."
  );

You can create a partial prompt:

const prompt = new PromptTemplate({
  template:
    "Explain {topic} in {language}.",
  inputVariables: ["topic"],
  partialVariables: {
    language: "English",
  },
});

Now:

const formattedPrompt =
  await prompt.format({
    topic: "Node.js",
  });

Output:

Explain Node.js in English.

This reduces duplication when some values remain constant.


Using pipe()

Without pipe():

const formattedPrompt =
  await prompt.format({
    topic: "Node.js",
  });

const response =
  await openAIModel.invoke(
    formattedPrompt
  );

console.log(response.content);

With pipe():

const chain =
  prompt.pipe(openAIModel);

const response =
  await chain.invoke({
    topic: "Node.js",
  });

console.log(response.content);

pipe() automatically formats the prompt and sends it to the model.


pipe() with Gemini

const chain =
  prompt.pipe(geminiModel);

const response =
  await chain.invoke({
    topic: "React",
  });

console.log(response.content);

The prompt logic remains exactly the same.


Real-World Example: AI Blog Generator

const prompt =
  PromptTemplate.fromTemplate(
    "Write a detailed blog on {topic}."
  );

const chain =
  prompt.pipe(openAIModel);

const response =
  await chain.invoke({
    topic: "Node.js Streams",
  });

console.log(response.content);

Real-World Example: Interview Question Generator

const prompt =
  PromptTemplate.fromTemplate(
    "Generate {count} interview questions on {topic}."
  );

const chain =
  prompt.pipe(geminiModel);

const response =
  await chain.invoke({
    topic: "JavaScript",
    count: 10,
  });

console.log(response.content);

Best Practices

  • Keep prompts reusable and generic.
  • Use descriptive variable names.
  • Use partial variables for values that don't change.
  • Prefer pipe() for cleaner code.
  • Store complex prompts in separate files for large projects.

Common Beginner Mistakes

  • Hardcoding prompt strings.
  • Forgetting to provide values for required variables.
  • Using different variable names in the template and input object.
  • Calling format() before pipe() unnecessarily.
  • Creating separate templates for each provider instead of reusing the same prompt.

Final Thoughts

PromptTemplate is one of the core building blocks of LangChain.js. It helps you create dynamic, reusable prompts instead of hardcoding prompt strings throughout your application.

By combining variables, multiple variables, partial variables, and pipe(), you can write cleaner, more maintainable code that works seamlessly with both OpenAI and Google Gemini.

As your applications grow, PromptTemplates become an essential part of building scalable AI workflows.

Happy Coding! 🚀