DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Classes / PromptTemplate
Prompt Template langchain-core Beginner

PromptTemplate: Reference Guide

By DevShelfHub

Create reusable text prompts with variable placeholders.

What is PromptTemplate?

PromptTemplate is the foundational string-formatting primitive in LangChain. It wraps a Python f-string-style template with curly-brace placeholders and validates that all required input variables are supplied before formatting. Unlike Python's str.format(), PromptTemplate raises a validation error if a required variable is missing—immediately rather than silently producing a broken prompt.

PromptTemplate works with any model that accepts a single string input. For chat models (ChatOpenAI, ChatAnthropic), prefer ChatPromptTemplate, which produces structured message lists. PromptTemplate.from_template() is the quickest constructor—it infers input_variables from the curly-brace placeholders automatically, so you don't need to list them manually.

Partial variables let you freeze some inputs and leave others open. partial() returns a new PromptTemplate with the supplied variables pre-filled—useful when the system context is fixed but the user query changes per invocation. You can also pass a callable as a partial variable, and LangChain will call it at format time.

When to Use

You need a simple template with variables. Use PromptTemplate for string-based templates.

Use Cases

  • Simple text prompts
  • Template reusability
  • Variable insertion
  • Prompt composition
  • Few-shot templates
  • Dynamic prompts

Key Features

  • Simple {variable} syntax
  • Validation
  • Composable
  • Format support
  • Reusable
  • LCEL-compatible

When NOT to Use

For chat models with roles—use ChatPromptTemplate.

Notes

Use ChatPromptTemplate for chat models

PromptTemplate produces a plain string, which is wrapped in a HumanMessage automatically when passed to a chat model. For precise system/user/assistant role control, use ChatPromptTemplate.from_messages() instead.

from_template infers input_variables

PromptTemplate.from_template("Hello {name}") automatically sets input_variables=["name"]. Only pass input_variables explicitly when some curly braces should not be treated as placeholders—escape them as {{ }}.

Partial variables for shared context

template.partial(context="...") binds one variable while leaving others open. The result is a new PromptTemplate—the original is unchanged. You can chain .partial() calls and pass a callable to compute the value at format time.

Template format: f-string vs jinja2

Default template_format="f-string". Switch to template_format="jinja2" to use Jinja2 syntax ({% if %}, {% for %}) for conditional prompts. Escape literal braces as {{ }} in Jinja2 templates to avoid conflicts with variable syntax.

Import

python
from langchain_core.prompts import PromptTemplate

Key Parameters

Parameter Type Default Purpose
template str None Template string with {variables}
input_variables List[str] None List of variable names

Code Examples

Simple Template

python
template = 'Tell me a {topic} joke'
pt = PromptTemplate(template=template, input_variables=['topic'])
result = pt.invoke({'topic': 'programming'})

Partial Variables

python
base = PromptTemplate.from_template('You are a {role}. Answer: {question}')
# Freeze the role, leave question open
expert_pt = base.partial(role='Python expert')
result = expert_pt.invoke({'question': 'What is a generator?'})

Jinja2 Conditional Template

python
from langchain_core.prompts import PromptTemplate
# Jinja2 allows conditionals
pt = PromptTemplate(
    template='Answer in {lang}.{% if formal %} Be formal.{% endif %} {{question}}',
    input_variables=['lang', 'formal', 'question'],
    template_format='jinja2'
)

Common Mistakes

❌ Not matching variables in template and input_variables

✅ Ensure every {var} in template is in input_variables

Alternatives

Class When to Use
ChatPromptTemplate For chat models with role-based messages

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with PromptTemplate and the wider framework.

PromptTemplate FAQ

What is PromptTemplate in LangChain?

Create reusable text prompts with variable placeholders. PromptTemplate is the foundational string-formatting primitive in LangChain. It wraps a Python f-string-style template with curly-brace placeholders and validates that all required input variables are supplied before formatting. Unlike Python's str.format(), PromptTemplate raises a validation error if a required variable is missing—immediately rather than silently producing a broken prompt. PromptTemplate works with any model that accepts a single string input. For chat mode…

Which package provides PromptTemplate?

DevShelfHub documents PromptTemplate from the langchain-core package. Pin your installed LangChain version and match imports to the snippet on this page.

When should I use PromptTemplate?

You need a simple template with variables. Use PromptTemplate for string-based templates.

When should I avoid using PromptTemplate?

For chat models with roles—use ChatPromptTemplate.

How do I import PromptTemplate in Python?

from langchain_core.prompts import PromptTemplate

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.