AI / LangGraph LangChain Interview questions
What are PromptTemplates in LangChain?
PromptTemplates are objects that format dynamic inputs into the correct structure before passing them to a model. Instead of building prompt strings with f-strings scattered across your codebase, templates give you reusable, testable, versionable prompt construction with named variables.
There are two main types:
- PromptTemplate — produces a plain text string. Best for LLMs (non-chat models):
from langchain_core.prompts import PromptTemplate pt = PromptTemplate.from_template("Summarise this in {n} sentences: {text}") print(pt.format(n=2, text="LangChain is..."))
- ChatPromptTemplate — produces a list of typed messages. Best for Chat Models:
from langchain_core.prompts import ChatPromptTemplate chat_pt = ChatPromptTemplate.from_messages([ ("system", "You are a {role}."), ("human", "{user_input}"), ]) messages = chat_pt.format_messages(role="poet", user_input="Write about the sea.")
MessagesPlaceholder is used inside a ChatPromptTemplate to insert a variable-length list of messages — useful for injecting conversation history. partial() lets you pre-fill some variables while leaving others to be filled at call time, which is handy for re-usable templates across different contexts.
More Related questions...