Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 32 ยท Models, Prompts and Parsers

Few-Shot Prompts and Message Placeholders

Teach a model by example with few-shot prompts, select the most relevant examples dynamically, and use MessagesPlaceholder to inject chat history into LangChain prompts.

Instructions alone often leave a model guessing about format and tone. Few-shot prompting adds a handful of worked examples to the prompt, and the model imitates them. It is the cheapest way to improve consistency, often better than rewriting the instructions. The second half of this lesson covers MessagesPlaceholder, the part of a template that holds a whole list of messages, which is how chat history enters a prompt.

Why examples work

A model predicts what comes next given everything before it. If the prompt shows three pairs of "ticket text, category", the most likely continuation of a fourth ticket is its category in the same style. Examples communicate things that are hard to describe: label spelling, length, level of detail, edge cases.

A good set of examples is small (2 to 8), diverse (cover the different categories, including a tricky one), correct (a wrong example teaches the wrong thing) and in the exact format you want back.

FewShotChatMessagePromptTemplate

LangChain builds the example block from a list of dictionaries and a template for one example:

Python
from langchain_core.prompts import (
    ChatPromptTemplate, FewShotChatMessagePromptTemplate,
)

examples = [
    {"ticket": "I was charged twice this month", "label": "billing"},
    {"ticket": "The app crashes when I open settings", "label": "bug"},
    {"ticket": "Can you add dark mode?", "label": "feature_request"},
]

example_prompt = ChatPromptTemplate.from_messages([
    ("human", "{ticket}"),
    ("ai", "{label}"),
])

few_shot = FewShotChatMessagePromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
)

final_prompt = ChatPromptTemplate.from_messages([
    ("system", "Classify the support ticket into one label."),
    few_shot,
    ("human", "{ticket}"),
])

The model sees alternating human/AI pairs, as if it had already answered three tickets correctly, and then a new human message. Each example becomes real chat turns, which many models follow more reliably than examples stuffed into one paragraph.

For plain completion-style prompts there is a string version, FewShotPromptTemplate, with prefix, examples and suffix parts. For chat models, prefer the chat version.

Building the example block (runnable)

Rendering examples is just a loop. This stand-in produces the same message list that the real template would.

Python Stand-in: assembling a few-shot chat prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Selecting examples dynamically

A fixed list works for a small task. With hundreds of labeled examples you cannot send them all (cost, and a longer prompt dilutes attention). An example selector picks the few most relevant for each input:

Python
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings
from langchain_core.vectorstores import InMemoryVectorStore

selector = SemanticSimilarityExampleSelector.from_examples(
    all_examples,
    OpenAIEmbeddings(model="text-embedding-3-small"),
    InMemoryVectorStore,
    k=3,
)
few_shot = FewShotChatMessagePromptTemplate(
    example_selector=selector,
    input_variables=["ticket"],
    example_prompt=example_prompt,
)

The selector embeds the new ticket and returns the k most similar stored examples. It uses the same embedding and vector-store ideas as the retrieval chapter. A cheaper selector, LengthBasedExampleSelector, just fits as many examples as a length budget allows.

Here is the selection idea with a crude similarity: how many words the new ticket shares with each example.

Python Stand-in: pick the most similar examples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real selectors use embeddings, which match meaning rather than shared words, so "refund my payment" can still find a billing example. The mechanism is otherwise identical.

MessagesPlaceholder: injecting a list of messages

Template variables hold strings. A conversation, though, is a list of messages. MessagesPlaceholder reserves a slot in a chat template for exactly that:

Python
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder("history"),
    ("human", "{question}"),
])

prompt.invoke({
    "history": [("human", "My name is Rin."), ("ai", "Nice to meet you, Rin!")],
    "question": "What is my name?",
})

The list you pass under history is spliced in at that position, so the model sees the earlier turns. Pass optional=True if the key may be missing on the first turn. The same pattern holds the running agent_scratchpad of tool calls in older agent code. The shorthand ("placeholder", "{history}") does the same thing.

Python Stand-in: a placeholder that splices history in

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Too many examples. Five good ones beat thirty mediocre ones; extras cost tokens and may bias the model.
  • Examples that disagree with the instructions. The model follows the examples.
  • Leaking test data into examples, which makes evaluation look better than reality.
  • Passing a string where MessagesPlaceholder expects a list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is few-shot prompting?

    Few-shot means giving the model a few input-output examples inside the prompt, with no training.
  2. 2. What does an example selector do?

    Selectors such as SemanticSimilarityExampleSelector keep prompts short by choosing only the best k examples.
  3. 3. What goes into a MessagesPlaceholder slot?

    The placeholder splices a whole list of messages into the template at that position.
  4. 4. Why use optional=True on a history placeholder?

    Without optional=True a missing history key is an error; with it the slot is simply empty.

Summary

  • Few-shot prompts show the model a few correct input-output examples to imitate.
  • FewShotChatMessagePromptTemplate turns example dicts into human/AI message pairs.
  • Example selectors choose the most relevant examples from a large pool, often via embeddings.
  • MessagesPlaceholder inserts a list of messages, the standard way to add chat history.
  • Keep examples few, correct, diverse and consistent with your instructions.

Next: Output Parsers