Bird
Raised Fist0
LangChainframework~20 mins

Few-shot prompt templates in LangChain - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Few-shot Prompt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this few-shot prompt template code?
Consider this LangChain few-shot prompt template code snippet. What will be the final prompt text after rendering?
LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

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

example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nResponse: {output}"
)

few_shot = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="You are a helpful assistant.",
    suffix="Input: {user_input}\nResponse:",
    input_variables=["user_input"]
)

final_prompt = few_shot.format(user_input="Hello")
print(final_prompt)
A
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Input: Hello
Response:
B
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Input: Hello
Response: Hi there!
C
You are a helpful assistant.
Input: Hello
Response: Hi there!
Input: Bye
Response: Goodbye!
Response: Hello
Input:
D
You are a helpful assistant.
Input: Hello
Response: Goodbye!
Input: Bye
Response: Hi there!
Input: Hello
Response:
Attempts:
2 left
💡 Hint
Look at how the examples are formatted and how the suffix uses the user input.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a FewShotPromptTemplate with multiple input variables?
You want to create a FewShotPromptTemplate that uses examples with inputs 'question' and 'answer', and a suffix that takes 'new_question'. Which code snippet is correct?
A
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["answer"]
)
B
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {question}\nA:",
  input_variables=["new_question"]
)
C
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["question"]
)
D
FewShotPromptTemplate(
  examples=examples,
  example_prompt=PromptTemplate(input_variables=["question", "answer"], template="Q: {question}\nA: {answer}"),
  prefix="Answer the questions.",
  suffix="Q: {new_question}\nA:",
  input_variables=["new_question"]
)
Attempts:
2 left
💡 Hint
Check that the suffix uses the same variable name as in input_variables.
🔧 Debug
advanced
2:00remaining
What error does this FewShotPromptTemplate code raise?
This code tries to create a FewShotPromptTemplate but fails. What error will it raise?
LangChain
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

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

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

few_shot = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Start conversation.",
    suffix="Input: {user_input}\nOutput:",
    input_variables=["user_input", "extra"]
)

few_shot.format(user_input="Hi")
ANo error, outputs the formatted prompt
BKeyError: 'extra'
CValueError: input_variables contains variables not in suffix or prefix
DTypeError: format() missing 1 required positional argument
Attempts:
2 left
💡 Hint
Check which variables are used in suffix and prefix versus input_variables.
state_output
advanced
2:00remaining
What is the number of example blocks in the rendered prompt?
Given this FewShotPromptTemplate with 3 examples, how many example blocks appear in the final prompt string?
LangChain
examples = [
  {"q": "What is AI?", "a": "Artificial Intelligence."},
  {"q": "What is ML?", "a": "Machine Learning."},
  {"q": "What is NLP?", "a": "Natural Language Processing."}
]

example_prompt = PromptTemplate(
  input_variables=["q", "a"],
  template="Q: {q}\nA: {a}"
)

few_shot = FewShotPromptTemplate(
  examples=examples,
  example_prompt=example_prompt,
  prefix="Answer the following questions.",
  suffix="Q: {new_q}\nA:",
  input_variables=["new_q"]
)

prompt_text = few_shot.format(new_q="What is Python?")
A4
B1
C3
D0
Attempts:
2 left
💡 Hint
Count how many example dictionaries are in the examples list.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of 'example_prompt' in FewShotPromptTemplate?
In LangChain's FewShotPromptTemplate, what is the main purpose of the 'example_prompt' parameter?
AIt defines how each example dictionary is formatted into text before being inserted into the prompt.
BIt specifies the variables required by the suffix template for new inputs.
CIt controls the order in which examples appear in the final prompt.
DIt validates the types of inputs passed to the FewShotPromptTemplate.
Attempts:
2 left
💡 Hint
Think about how examples become text in the prompt.

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