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




















