Bird
0
0

What is the issue with this code snippet?

medium📝 Debug Q6 of 15
LangChain - Prompt Templates
What is the issue with this code snippet?
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(template="Greetings, {user}!")
print(prompt.format(username="Alice"))
AThe template string is missing required curly braces
BPromptTemplate cannot be imported from langchain.prompts
CThe variable name in format() does not match the template placeholder
Dformat() method cannot be used with PromptTemplate
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the template

    The template expects a variable named user.
  2. Step 2: Analyze the format call

    The format method is called with username="Alice", which does not match user.
  3. Step 3: Identify the error

    This mismatch causes a KeyError because the template variable is not provided.
  4. Final Answer:

    The variable name in format() does not match the template placeholder -> Option C
  5. Quick Check:

    Variable names must match exactly in template and format [OK]
Quick Trick: Match variable names exactly in template and format [OK]
Common Mistakes:
  • Using different variable names in format() than in template
  • Assuming format() accepts any variable names
  • Ignoring KeyError from missing variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes