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.

Steps to create a LangChain prompt template:

  1. Create prompt_template.py with the message roles and placeholders for the reusable prompt.
    $ 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.

  2. Append the runtime values and template invocation to prompt_template.py.
    $ cat >> prompt_template.py <<'PY'
     
    values = {
        "change": "automated snapshot cleanup",
        "audience": "database operators",
        "sentences": 2,
    }
    prompt_value = prompt.invoke(values)
    PY
  3. Append the output loop that prints each formatted message with its role.
    $ 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
  4. Confirm prompt_template.py contains the completed template, values, invocation, and output loop.
    prompt_template.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}")
  5. Run prompt_template.py to render the fully populated system and human messages.
    $ 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.