Bird
0
0

Identify the issue in this LangChain code snippet:

medium📝 Debug Q6 of 15
LangChain - Fundamentals
Identify the issue in this LangChain code snippet:
from langchain import PromptTemplate
prompt = PromptTemplate(template="Welcome, {user}!")
print(prompt.format(username="Bob"))
AThe template string is missing required curly braces
BPromptTemplate cannot be imported from langchain
CThe input variable name in format() does not match the template variable
DThe print statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check template variable

    The template uses '{user}' as the variable.
  2. Step 2: Check format() argument

    The code calls prompt.format(username="Bob"), which uses 'username' instead of 'user'.
  3. Step 3: Identify mismatch

    This mismatch causes a KeyError because 'user' is expected but 'username' is provided.
  4. Final Answer:

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

    Variable names must match exactly [OK]
Quick Trick: Template and format variable names must match [OK]
Common Mistakes:
  • Using different variable names in template and format
  • Assuming import error without checking code
  • Overlooking syntax correctness in print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes