PromptTemplate

What is PromptTemplate?

A PromptTemplate is a reusable prompt with placeholders (variables). Instead of hardcoding prompts, you create a template and fill in values dynamically.

Without PromptTemplate, You might write:
prompt = f"""
Explain {topic} in simple terms.
"""

This works, but becomes messy when prompts get large.

With PromptTemplate,

# Define a template 
template = """
Explain {topic} in simple terms.
"""

# create a variable
topic = "RAG"

# Final prompt becomes:
Explain RAG in simple terms.

Real LangChain Example

from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template(
    "Explain {topic} in simple terms."
)

result = prompt.invoke(
    {"topic": "Delta Lake"}
)

print(result.text)

More Realistic Example

(1) Define Template

# Define Template

template = """
You are a Data Architect.

Question:
{question}

Answer in:
{language}
"""

(2) Define Variables:

# define variables
{
  "question": "What is Medallion Architecture?",
  "language": "English"
}

Generated Prompt looks like:

You are a Data Architect.

Question:
What is Medallion Architecture?

Answer in:
English

Final Takeaway

PromptTemplate = Prompt with variables.

It allows you to create one prompt structure and dynamically inject data.

This is one of the most frequently used components in LangChain, RAG, and Agent systems.

ChatPromptTemplate

Most modern Agent and RAG applications do not use the basic PromptTemplate. Instead, they rely on ChatPromptTemplate, because modern LLMs (such as GPT models) are designed around a message-based structure (system / user / assistant roles).

ChatPromptTemplate allows developers to define structured conversational prompts, for example:

  • System message (role definition)
  • User message (input question)
  • Optional assistant context or memory

This structure is essential for:

  • RAG pipelines (injecting retrieved context as messages)
  • Agent systems (multi-step reasoning and tool use)
  • Production-grade LLM applications

In short:
👉 PromptTemplate = simple text template
👉 ChatPromptTemplate = structured chat-based template (industry standard for agents and RAG)