A chat model receives ordered messages rather than one undifferentiated block of text. A reusable LangChain prompt template preserves those message roles while leaving selected details open for each invocation.
The ChatPromptTemplate class represents the system and human messages separately, and placeholders such as {change} become required input variables. It is available from langchain_core.prompts without a model-provider package.
Formatting the template locally does not call a model or require an API key. A successful invocation returns a ChatPromptValue whose messages show the exact text that a later model call would receive; a missing required value raises an error before any provider request is made.
$ cat > prompt_template.py <<'PY' from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ("system", "You write release notes for infrastructure teams."), ( "human", "Summarize {change} for {audience} in no more than {sentences} sentences.", ), ] ) PY
The default f-string template format substitutes named values without evaluating a template language. Jinja2 templates from untrusted sources can execute unsafe expressions and should not be accepted as user input.
$ cat >> prompt_template.py <<'PY' values = { "change": "automated snapshot cleanup", "audience": "database operators", "sentences": 2, } prompt_value = prompt.invoke(values) PY
$ cat >> prompt_template.py <<'PY' print("Required variables:", ", ".join(prompt.input_variables)) for message in prompt_value.to_messages(): print(f"{message.type}: {message.content}") PY
from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages( [ ("system", "You write release notes for infrastructure teams."), ( "human", "Summarize {change} for {audience} in no more than {sentences} sentences.", ), ] ) values = { "change": "automated snapshot cleanup", "audience": "database operators", "sentences": 2, } prompt_value = prompt.invoke(values) print("Required variables:", ", ".join(prompt.input_variables)) for message in prompt_value.to_messages(): print(f"{message.type}: {message.content}")
$ python3 prompt_template.py Required variables: audience, change, sentences system: You write release notes for infrastructure teams. human: Summarize automated snapshot cleanup for database operators in no more than 2 sentences.