Templates let you write a prompt once with blank spots (placeholders). You fill those spots with different values each time you use the template. This saves time and keeps prompts consistent.
from langchain.prompts import PromptTemplate template = PromptTemplate( input_variables=["animal"], template="Tell me a joke about a {animal}." ) print(template.format(animal="cat")) print(template.format(animal="dog"))
The template keeps the prompt structure but replaces the placeholder {animal} with the value you give each time you call format(). This lets you reuse the same prompt with different inputs.
The correct parameter name is input_variables. Placeholders in the template must be wrapped in curly braces {} to be replaced.
from langchain.prompts import PromptTemplate template = PromptTemplate( input_variables=["city", "activity"], template="In {city}, people enjoy {activity}." ) print(template.format(city="Paris"))
The format method requires all placeholders to be filled. Missing variables cause a KeyError because the template cannot replace them.
from langchain.prompts import PromptTemplate template = PromptTemplate( input_variables=["food"], template='I love {food}!' ) print(template.format(food='pizza'))
In Python, single or double quotes do not affect string content. Placeholders inside single quotes are replaced correctly by format(). The code prints 'I love pizza!'.