Prompt templates
A prompt template formats named inputs into a prompt value. ChatPromptTemplate preserves message roles instead of building one large string by hand.
Tiny example
Section titled “Tiny example”from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([ ("system", "Answer only from this policy:\n{policy}"), ("human", "Order {order_id}: {question}"),])
messages = prompt.invoke({ "policy": "Returns are accepted for 30 days.", "order_id": "A-19", "question": "Can I return it after 45 days?",})Remember the flow
Section titled “Remember the flow”- Write stable instructions once.
- Name each changing value.
- Format with a dictionary.
- Pass the result to a model or compose it with
|.
Failure note
Section titled “Failure note”Templates format text; they do not make untrusted text safe. Retrieved pages and user input can contain instructions. Keep authorization outside the prompt, mark data clearly, and test prompt-injection cases.