0
0
LangChainframework~5 mins

Why templates create reusable prompts in LangChain

Choose your learning style9 modes available
Introduction

Templates help you write prompts once and use them many times. This saves time and keeps your prompts clear and organized.

When you want to ask similar questions but with different details.
When you need to keep your prompt style consistent across many uses.
When you want to quickly change parts of a prompt without rewriting everything.
When working in a team to share standard prompts easily.
When automating tasks that require repeated prompt formats.
Syntax
LangChain
from langchain.prompts import PromptTemplate

template = PromptTemplate(
    input_variables=["name"],
    template="Hello, {name}! How can I help you today?"
)

prompt_text = template.format(name="Alice")

input_variables are placeholders you fill later.

Use {variable_name} inside the template string to mark where values go.

Examples
This template asks about weather in different cities by changing the city name.
LangChain
template = PromptTemplate(
    input_variables=["city"],
    template="Tell me about the weather in {city}."
)
prompt_text = template.format(city="Paris")
This template uses two variables to create a sentence about a product and its price.
LangChain
template = PromptTemplate(
    input_variables=["product", "price"],
    template="The {product} costs {price} dollars."
)
prompt_text = template.format(product="book", price="15")
Sample Program

This program shows how one template can greet different users by changing the name each time.

LangChain
from langchain.prompts import PromptTemplate

# Create a template with one variable
template = PromptTemplate(
    input_variables=["user_name"],
    template="Welcome, {user_name}! Ready to start your journey?"
)

# Use the template with different names
print(template.format(user_name="Sam"))
print(template.format(user_name="Lily"))
OutputSuccess
Important Notes

Templates keep your prompts neat and easy to update.

Always name your variables clearly to avoid confusion.

You can reuse one template many times with different inputs.

Summary

Templates let you write prompts once and reuse them easily.

They use placeholders to insert different information each time.

This helps save time and keeps your prompts consistent.