Bird
Raised Fist0
LangChainframework~20 mins

PromptTemplate basics 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
🎖️
PromptTemplate Mastery
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 PromptTemplate render?
Given the PromptTemplate below, what will be the rendered output when input variable 'name' is 'Alice'?
LangChain
from langchain.prompts import PromptTemplate

template = "Hello, {name}! Welcome to LangChain."
prompt = PromptTemplate(template=template, input_variables=["name"])
output = prompt.format(name="Alice")
print(output)
AHello, Alice! Welcome to {LangChain}.
BHello, {name}! Welcome to LangChain.
CHello, name! Welcome to LangChain.
DHello, Alice! Welcome to LangChain.
Attempts:
2 left
💡 Hint
Look at how the variable 'name' is used inside curly braces and how format replaces it.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a PromptTemplate with two input variables?
You want to create a PromptTemplate with variables 'city' and 'weather'. Which code snippet is correct?
APromptTemplate(template="The weather in {city} is {weather}.", input_variables="city, weather")
BPromptTemplate(template="The weather in {city} is {weather}.", input_variables=["city", "weather"])
CPromptTemplate(template="The weather in {city} is {weather}.", input_variables={"city", "weather"})
DPromptTemplate(template="The weather in {city} is {weather}.", input_variables=[city, weather])
Attempts:
2 left
💡 Hint
input_variables must be a list of strings.
state_output
advanced
2:00remaining
What is the output of this PromptTemplate with missing variable?
What happens when you try to format a PromptTemplate but omit one required variable?
LangChain
from langchain.prompts import PromptTemplate

template = "Today in {city}, the temperature is {temp} degrees."
prompt = PromptTemplate(template=template, input_variables=["city", "temp"])
output = prompt.format(city="Paris")
print(output)
ARaises a KeyError because 'temp' is missing
BOutputs: Today in Paris, the temperature is {temp} degrees.
COutputs: Today in Paris, the temperature is degrees.
DOutputs: Today in {city}, the temperature is {temp} degrees.
Attempts:
2 left
💡 Hint
Check what happens if a required variable is not provided to format.
🧠 Conceptual
advanced
2:00remaining
Which statement about PromptTemplate input_variables is true?
Choose the correct statement about the input_variables parameter in PromptTemplate.
Ainput_variables must list all variables used in the template string exactly.
Binput_variables can be omitted if the template uses no variables.
Cinput_variables can include variables not present in the template string.
Dinput_variables must be a dictionary mapping variable names to default values.
Attempts:
2 left
💡 Hint
Think about how PromptTemplate validates variables.
🔧 Debug
expert
2:00remaining
Why does this PromptTemplate raise a ValueError?
Examine the code and choose the reason for the ValueError.
LangChain
from langchain.prompts import PromptTemplate

template = "Hello, {name}! Your age is {age}."
prompt = PromptTemplate(template=template, input_variables=["name", "age", "location"])
output = prompt.format(name="Bob", age=30, location="NY")
print(output)
Aformat arguments do not match input_variables types
Bformat is missing a required variable 'location', causing ValueError
Cinput_variables includes 'location' which is not in the template, causing ValueError
Dtemplate string has syntax error with curly braces
Attempts:
2 left
💡 Hint
Check if input_variables matches variables in the template exactly.

Practice

(1/5)
1. What is the main purpose of PromptTemplate in langchain?
easy
A. To create message templates with placeholders for dynamic content
B. To execute machine learning models directly
C. To store data in a database
D. To visualize data in charts

Solution

  1. Step 1: Understand the role of PromptTemplate

    PromptTemplate is designed to create text templates that include placeholders for variables.
  2. Step 2: Identify its main use

    It helps organize prompts by allowing you to fill in placeholders later, making prompt reuse easier.
  3. Final Answer:

    To create message templates with placeholders for dynamic content -> Option A
  4. Quick Check:

    PromptTemplate = Templates with placeholders [OK]
Hint: PromptTemplate = template with blanks to fill [OK]
Common Mistakes:
  • Thinking PromptTemplate runs models
  • Confusing it with data storage
  • Assuming it creates visual charts
2. Which of the following is the correct way to create a PromptTemplate with a placeholder named name?
easy
A. PromptTemplate.from_template("Hello, <name>!")
B. PromptTemplate.from_template("Hello, $name!")
C. PromptTemplate.from_template("Hello, {name}!")
D. PromptTemplate.from_template("Hello, %name%!")

Solution

  1. Step 1: Recall placeholder syntax in PromptTemplate

    Langchain uses curly braces {} to mark placeholders in templates.
  2. Step 2: Match the syntax to the options

    Only PromptTemplate.from_template("Hello, {name}!") uses curly braces correctly: {name}.
  3. Final Answer:

    PromptTemplate.from_template("Hello, {name}!") -> Option C
  4. Quick Check:

    Placeholders use curly braces {} [OK]
Hint: Placeholders always use curly braces {} [OK]
Common Mistakes:
  • Using $ or % instead of {} for placeholders
  • Using angle brackets <> which are invalid
  • Confusing placeholder syntax with other languages
3. Given the code:
from langchain.prompts import PromptTemplate
prompt = PromptTemplate.from_template("Hello, {name}!")
result = prompt.format(name="Alice")
print(result)

What will be printed?
medium
A. Hello, {name}!
B. Hello, Alice!
C. Hello, name!
D. Error: missing argument

Solution

  1. Step 1: Understand the template and format method

    The template has a placeholder {name}. The format method fills it with the value "Alice".
  2. Step 2: Determine the output of print(result)

    After formatting, the placeholder is replaced, so the output is "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    format() replaces {name} with "Alice" [OK]
Hint: format() fills placeholders with given values [OK]
Common Mistakes:
  • Printing template without formatting
  • Confusing placeholder name with literal text
  • Expecting an error without missing arguments
4. What is the error in this code snippet?
from langchain.prompts import PromptTemplate
prompt = PromptTemplate.from_template("Hi, {user}!")
result = prompt.format(name="Bob")
print(result)
medium
A. KeyError because 'user' placeholder is not provided
B. No error; output is 'Hi, Bob!'
C. SyntaxError due to wrong placeholder syntax
D. TypeError because format() is missing arguments

Solution

  1. Step 1: Check placeholder and format argument names

    The template uses {user} but format() provides name="Bob" which does not match.
  2. Step 2: Understand the error caused

    Since {user} is not given a value, a KeyError occurs during formatting.
  3. Final Answer:

    KeyError because 'user' placeholder is not provided -> Option A
  4. Quick Check:

    Placeholder name must match format() keys [OK]
Hint: Placeholder and format keys must match exactly [OK]
Common Mistakes:
  • Assuming format() keys can differ from placeholders
  • Thinking it's a syntax error
  • Expecting no error when keys mismatch
5. You want to create a PromptTemplate that asks for a user's name and age, then formats a greeting. Which code correctly defines and uses this template?
hard
A. prompt = PromptTemplate.from_template("Hello {name}, you are {age} years old.") result = prompt.format(user="Eva", years=30) print(result)
B. prompt = PromptTemplate.from_template("Hello {name}, you are {age} years old.") result = prompt.format(name="Eva") print(result)
C. prompt = PromptTemplate.from_template("Hello {name}, you are {age} years old.") result = prompt.format("Eva", 30) print(result)
D. prompt = PromptTemplate.from_template("Hello {name}, you are {age} years old.") result = prompt.format(name="Eva", age=30) print(result)

Solution

  1. Step 1: Check template placeholders

    The template has placeholders {name} and {age} which must be provided as keyword arguments in format().
  2. Step 2: Verify format() usage

    prompt = PromptTemplate.from_template("Hello {name}, you are {age} years old.") result = prompt.format(name="Eva", age=30) print(result) correctly passes name="Eva" and age=30 matching placeholders. Others miss arguments or use wrong keys or positional args.
  3. Final Answer:

    Correctly defines and uses placeholders with matching keys -> Option D
  4. Quick Check:

    Match placeholders and format() keys exactly [OK]
Hint: Pass all placeholders as keyword args in format() [OK]
Common Mistakes:
  • Missing required placeholder arguments
  • Using wrong argument names in format()
  • Passing positional args instead of keywords