Bird
Raised Fist0
LangChainframework~10 mins

Few-shot prompt templates in LangChain - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Few-shot prompt templates
Start: Define examples
Create prompt template with examples
Add user input
Generate full prompt
Send prompt to model
Receive model output
Return output to user
This flow shows how few-shot prompt templates combine example pairs with user input to create a prompt for the model, then get and return the output.
Execution Sample
LangChain
from langchain.prompts import PromptTemplate, FewShotPromptTemplate

examples = [
  {"input": "Hello", "output": "Hi!"},
  {"input": "Bye", "output": "Goodbye!"}
]

example_template = PromptTemplate(
  input_variables=["input", "output"],
  template="Input: {input}\nOutput: {output}\n"
)

prompt = FewShotPromptTemplate(
  examples=examples,
  input_variables=["input"],
  example_prompt=example_template,
  suffix="Input: {input}\nOutput: "
)

full_prompt = prompt.format(input="Thanks")
This code creates a few-shot prompt with two examples and formats it with a new input.
Execution Table
StepActionInputPrompt ContentOutput
1Define examples[{"input": "Hello", "output": "Hi!"}, {"input": "Bye", "output": "Goodbye!"}]N/AExamples stored
2Create FewShotPromptTemplateexamples + input_variables + example_promptTemplate ready with placeholdersTemplate object created
3Format prompt with input"Thanks"Includes examples + 'Thanks' inputFull prompt string generated
4Send prompt to modelFull prompt stringPrompt sent to language modelModel processes prompt
5Receive model outputModel responseN/A"You're welcome!" (example output)
6Return outputModel outputN/A"You're welcome!" returned to user
💡 Process ends after model output is returned to the user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
examplesempty[{"input": "Hello", "output": "Hi!"}, {"input": "Bye", "output": "Goodbye!"}][same][same][same][same]
promptundefinedundefinedFewShotPromptTemplate objectFewShotPromptTemplate objectFewShotPromptTemplate objectFewShotPromptTemplate object
full_promptundefinedundefinedundefinedFull prompt string with examples and inputFull prompt stringFull prompt string
model_outputundefinedundefinedundefinedundefined"You're welcome!""You're welcome!"
Key Moments - 3 Insights
Why do we include examples in the prompt before the user input?
Including examples shows the model how to respond, guiding it to produce similar answers. See execution_table step 3 where examples are combined with input.
What happens if we forget to format the prompt with the user input?
The prompt will only have examples but no new input, so the model won't know what to answer. Refer to execution_table step 3 where formatting adds the input.
Is the model output always the same for the same input?
No, the model can produce different outputs due to randomness or settings. The example output in step 5 is just one possible answer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does the 'full_prompt' contain?
AOnly the examples without user input
BOnly the new user input without examples
CExamples plus the new user input formatted together
DAn empty string
💡 Hint
Check the 'Prompt Content' column at step 3 in the execution_table.
At which step does the model generate a response?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Output' column in the execution_table for when the model output appears.
If we add more examples, how does the 'full_prompt' change at step 3?
AIt stays the same size
BIt includes more example pairs before the user input
CIt removes the user input
DIt becomes empty
💡 Hint
Refer to how examples are combined with input in the 'Prompt Content' at step 3.
Concept Snapshot
Few-shot prompt templates combine example input-output pairs with new user input.
They create a prompt showing examples first, then the new input.
This guides the model to respond similarly.
Use format() to insert user input into the template.
Send the full prompt to the model to get output.
Useful for teaching the model by example.
Full Transcript
Few-shot prompt templates work by first defining example input-output pairs. Then, these examples are combined with the user's new input to create a full prompt. This prompt is sent to the language model, which generates a response based on the examples and input. The process involves creating the template, formatting it with input, sending it to the model, and returning the output. This method helps the model understand how to respond by showing examples first.

Practice

(1/5)
1. What is the main purpose of a few-shot prompt template in Langchain?
easy
A. To provide example prompts and responses to guide AI behavior
B. To store large datasets for training AI models
C. To execute code on the AI server
D. To create user interfaces for AI applications

Solution

  1. Step 1: Understand few-shot prompt templates

    Few-shot prompt templates include example prompts and responses to teach AI how to answer.
  2. Step 2: Identify the main purpose

    The main goal is to guide AI behavior by showing examples, not to store data or create interfaces.
  3. Final Answer:

    To provide example prompts and responses to guide AI behavior -> Option A
  4. Quick Check:

    Few-shot prompt templates guide AI with examples = A [OK]
Hint: Remember: few-shot means showing examples to teach AI [OK]
Common Mistakes:
  • Confusing prompt templates with data storage
  • Thinking they run code instead of guiding AI
  • Assuming they build UI components
2. Which of the following is the correct way to create a few-shot prompt template in Langchain?
easy
A. FewShotPromptTemplate(data=examples, prompt=example_prompt, suffix=prefix_text)
B. FewShotPromptTemplate(samples=examples, prompt_template=example_prompt, header=prefix_text)
C. FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text)
D. FewShotPromptTemplate(inputs=examples, prompt=example_prompt, footer=prefix_text)

Solution

  1. Step 1: Recall Langchain few-shot prompt syntax

    The correct constructor uses parameters: examples, example_prompt, and prefix.
  2. Step 2: Match parameters to options

    Only FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text) uses the exact parameter names required by Langchain's FewShotPromptTemplate.
  3. Final Answer:

    FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix=prefix_text) -> Option C
  4. Quick Check:

    Correct parameter names = B [OK]
Hint: Check parameter names exactly as in Langchain docs [OK]
Common Mistakes:
  • Using wrong parameter names like data or samples
  • Mixing prefix with suffix or footer
  • Confusing example_prompt with prompt_template
3. Given this code snippet, what will be the output of print(prompt_template.format(input="Translate to French: Hello") )?
examples = [{"input": "Hello", "output": "Bonjour"}]
example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
prompt_template = FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix="Translate English to French:\n", suffix="\nInput: {input}\nOutput:", input_variables=["input"])
medium
A. Translate English to French: Input: Translate to French: Hello Output:
B. Translate English to French: Input: Hello Output: Bonjour Translate to French: Hello
C. Input: Hello Output: Bonjour Translate English to French: Hello
D. Translate English to French: Input: Hello Output: Bonjour Input: Translate to French: Hello Output:

Solution

  1. Step 1: Understand few-shot prompt formatting

    The prompt includes the prefix, then example prompts formatted with example data, then the new input prompt.
  2. Step 2: Apply formatting to given input

    The prefix is "Translate English to French:", then example "Input: Hello\nOutput: Bonjour", then the new input "Input: Translate to French: Hello\nOutput:" (empty output to be filled by AI).
  3. Final Answer:

    Translate English to French: Input: Hello Output: Bonjour Input: Translate to French: Hello Output: -> Option D
  4. Quick Check:

    Prefix + example + new input prompt = C [OK]
Hint: Few-shot templates show prefix, examples, then new input [OK]
Common Mistakes:
  • Ignoring prefix text in output
  • Not formatting new input as 'Input: ... Output:'
  • Confusing example data with new input
4. What is the error in this code snippet that tries to create a few-shot prompt template?
examples = [{"input": "Hi", "output": "Salut"}]
example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
prompt_template = FewShotPromptTemplate(examples=examples, example_prompt=example_prompt, prefix="Translate:")
print(prompt_template.format(input="Hello"))
medium
A. PromptTemplate cannot have input_variables
B. Missing input_variables parameter in FewShotPromptTemplate constructor
C. Prefix must be a function, not a string
D. examples list should be empty for FewShotPromptTemplate

Solution

  1. Step 1: Check FewShotPromptTemplate required parameters

    FewShotPromptTemplate requires input_variables parameter to know which inputs to expect.
  2. Step 2: Identify missing parameter

    The code misses input_variables in FewShotPromptTemplate, causing an error when calling format.
  3. Final Answer:

    Missing input_variables parameter in FewShotPromptTemplate constructor -> Option B
  4. Quick Check:

    input_variables missing = D [OK]
Hint: Always include input_variables when creating prompt templates [OK]
Common Mistakes:
  • Omitting input_variables in FewShotPromptTemplate
  • Thinking prefix must be a function
  • Assuming examples can be empty
5. You want to create a few-shot prompt template that filters out examples with empty outputs before formatting. Which approach correctly applies this filtering in Langchain?
hard
A. Filter the examples list before passing it to FewShotPromptTemplate constructor
B. Use a custom example_prompt that skips empty outputs during formatting
C. Set prefix to None and rely on Langchain to ignore empty outputs
D. Pass all examples and filter outputs after calling prompt_template.format()

Solution

  1. Step 1: Understand filtering in few-shot templates

    FewShotPromptTemplate uses the examples list as-is; filtering must happen before passing examples.
  2. Step 2: Evaluate options for filtering

    Only filtering the examples list before creating the template ensures empty outputs are excluded properly.
  3. Final Answer:

    Filter the examples list before passing it to FewShotPromptTemplate constructor -> Option A
  4. Quick Check:

    Pre-filter examples before constructor = A [OK]
Hint: Filter examples before creating the prompt template [OK]
Common Mistakes:
  • Trying to filter inside example_prompt formatting
  • Assuming Langchain auto-filters empty outputs
  • Filtering after formatting instead of before