liveBook · Manning

Template

As explained in Hugging Face in Action

PromptTemplate is a LangChain class that represents a parameterized string template and formats user-provided input variables into a prompt.

A prompt template is a parameterized string that structures instructions or questions for an LLM. PromptTemplate replaces named input variables with user-provided values, producing a prompt that can be passed to a model. section 7.2.2

Create and run a simple template

from langchain import PromptTemplate prompt = PromptTemplate( template='Question: {question}\nAnswer:', input_variables=['question'] ) llm_chain = prompt | hub_llm | StrOutputParser() response = llm_chain.invoke({'question': 'What is LangChain?'}) print(response) The {question} placeholder must match the name listed in input_variables. Pipeline chaining sends the formatted prompt to the LLM and then converts the result to a string. section 7.2.2 section 7.2.4

Use prompt templates to control inputs and connect LLM workflows:

  • Declare every placeholder, such as question, history, context, or document, in input_variables. section 7.2.2 section 7.2.6
  • Add a history variable to preserve conversational context and pass the previous response when invoking the chain. section 7.2.6
  • Inject retrieved document context into a template before asking a local model to answer a question about personal data. section 12.1.8
  • Use templates with hosted or local models, including OpenAI, Hugging Face, and GPT4All integrations. section 12.1.8 section 7.2.8
Listing 7.1 Creating a simple PromptTemplate component
from langchain import PromptTemplate

template = '''
Question: {question}
Answer:
'''

prompt = PromptTemplate(
    template = template,
    input_variables = ['question']
)
prompt

Inspect a minimal Python construction that links the question placeholder to the PromptTemplate input. section 7.2.2

How Hugging Face in Action covers it

How deep it goes
Explains placeholders, input_variables, invocation, chaining, history, and retrieved context, but does not descend into template parsing internals.
Examples
Demonstrates question answering, conversational history, document retrieval, DataFrame queries, Langflow prompts, and weather-report generation.
What it assumes
Assumes basic Python and LLM usage while teaching prompt variables, template construction, chaining, and conversational context explicitly.
What you can do after
Build it from scratch using PromptTemplate, supply named variables, invoke chains, and connect prompts to local or hosted models.
How it connects
Connects prompt templates with LLMs, output parsers, conversation memory, vector embeddings, dataframes, and MCP tools.

Also explained in

Compare how 21 books explain template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.