ChatPromptTemplate constructs reusable chat prompts from a template and supplied values, allowing LangChain chains to insert dynamic inputs before invoking a model. section 6.1.1
Use it to:
- Define placeholders such as {vowelCount} and let the framework validate required values. section 6.1.1
- Reuse one prompt structure across modules while varying tone, length, content, or other inputs. section 6.1.1
- Pass a prompt object through runnable pipelines that continue into a language model and output parser. section 6.1.1
Use a template inside a runnable chain
const prompt = ChatPromptTemplate.fromTemplate("Show the number {vowelCount} two times."); const sequence = RunnableSequence.from([ RunnableLambda.from(toUpperCase), RunnableLambda.from(vowelCountFunction), prompt, model, new StringOutputParser(), ]); section 6.1.1
Listing 6.1 Example using runnable chains
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableLambda } from "@langchain/core/runnables";
import { ChatOpenAI } from "@langchain/openai";
import "dotenv/config";
const apiKey = process.env.OPENAI_API_KEY;
const toUpperCase = (input) => { #1
return {
uppercased: input.text.toUpperCase(),
};
};
const vowelCountFunction = (input) => { #2
const vowels = input.uppercased.match(/[AEIOU]/gi);
return {
vowelCount: vowels ? vowels.length : 0,
};
};
const model = new ChatOpenAI({ openAIApiKey: apiKey,
model: "gpt-4o" }); #3
const prompt = ChatPromptTemplate.fromTemplate("Show the number {vowelCount} two times."); #4
const chain = RunnableLambda.from(toUpperCase). #5
.pipe(RunnableLambda.from(vowelCountFunction))
.pipe(prompt)
.pipe(model)
.pipe(new StringOutputParser());
await chain.invoke({text: "hello world"})
.then((output) => { #6
console.log(output);
});
Inspect the complete runnable-chain listing, including the template, model, transformations, and output parser. section 6.1.1

