Hint: Use curly braces and list of strings for variables [OK]
Common Mistakes:
Forgetting quotes around variable names
Using wrong parameter name like variables instead of input_variables
Not using curly braces in template
3. Given the code:
template = PromptTemplate(template="Hello {user}, today is {day}.", input_variables=["user", "day"])
result = template.format(user="Alice", day="Monday")
print(result)
What will be printed?
medium
A. Hello Alice, today is Monday.
B. Hello {user}, today is {day}.
C. Hello user, today is day.
D. Error: Missing variables
Solution
Step 1: Understand how format fills variables
The format method replaces {user} with "Alice" and {day} with "Monday".
Step 2: Predict the final string after formatting
The placeholders are replaced, so the printed string is "Hello Alice, today is Monday."
Final Answer:
Hello Alice, today is Monday. -> Option A
Quick Check:
format replaces variables correctly = Hello Alice, today is Monday. [OK]
Hint: format() replaces placeholders with given values [OK]
Common Mistakes:
Printing template string without formatting
Confusing variable names with values
Expecting an error when variables are provided
4. What is wrong with this code snippet?
template = PromptTemplate(template="Hi {name}", input_variables=["name"])
result = template.format(nam="Bob")
print(result)
medium
A. input_variables should be a string, not a list
B. The template string is missing curly braces
C. format method cannot be used with PromptTemplate
D. The variable name in format is misspelled, causing a KeyError
Solution
Step 1: Compare variable names in template and format call
The template expects variable "name" but format uses "nam" which is incorrect.
Step 2: Understand the error caused by mismatch
This mismatch causes a KeyError because "name" is not provided in format arguments.
Final Answer:
The variable name in format is misspelled, causing a KeyError -> Option D
Quick Check:
Variable name mismatch = KeyError [OK]
Hint: 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
5. You want to create a PromptTemplate that dynamically inserts a user's name and their favorite color, but if the color is not provided, it should default to "blue". Which approach correctly handles this dynamic content with a default value?
hard
A. Use input_variables=["name", "color"] and call format with color="blue" if missing
B. Define template with {name} and {color}, but omit color from input_variables to default it
C. Use input_variables=["name"] only and write template as "Hello {name}, your color is blue"
D. Set input_variables=["name", "color"] and use a conditional expression inside template like {color or 'blue'}
Solution
Step 1: Understand how to provide default values
Langchain's PromptTemplate requires all variables listed in input_variables to be provided when formatting.
Step 2: Provide default value in code when calling format
By including color in input_variables and passing color="blue" if missing, you ensure the template fills correctly.
Final Answer:
Use input_variables=["name", "color"] and call format with color="blue" if missing -> Option A
Quick Check:
Default values handled in format call = Use input_variables=["name", "color"] and call format with color="blue" if missing [OK]
Hint: Provide all variables; set defaults when calling format [OK]
Common Mistakes:
Omitting variables from input_variables to default
Trying to use Python expressions inside template strings
Hardcoding default text instead of using variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.