Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
LangChain - Prompt Templates
What is wrong with this code snippet?
template = PromptTemplate(template="Hi {name}", input_variables=["name"])
result = template.format(nam="Bob")
print(result)
Ainput_variables should be a string, not a list
BThe template string is missing curly braces
Cformat method cannot be used with PromptTemplate
DThe variable name in format is misspelled, causing a KeyError
Step-by-Step Solution
Solution:
  1. Step 1: Compare variable names in template and format call

    The template expects variable "name" but format uses "nam" which is incorrect.
  2. Step 2: Understand the error caused by mismatch

    This mismatch causes a KeyError because "name" is not provided in format arguments.
  3. Final Answer:

    The variable name in format is misspelled, causing a KeyError -> Option D
  4. Quick Check:

    Variable name mismatch = KeyError [OK]
Quick Trick: Check variable names match exactly in template and format [OK]
Common Mistakes:
  • Assuming format ignores missing variables
  • Thinking input_variables must be a string
  • Believing format method is invalid here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes