Challenge - 5 Problems
LangChain Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does LangChain handle variable substitution in prompt templates?
Consider a LangChain prompt template that uses variables to dynamically insert user input. What will be the output if the variable is not provided when calling the template?
LangChain
from langchain.prompts import PromptTemplate prompt = PromptTemplate( input_variables=["name"], template="Hello, {name}! Welcome to LangChain." ) output = prompt.format()
Attempts:
2 left
💡 Hint
Think about how Python string formatting behaves when a required key is missing.
✗ Incorrect
LangChain's PromptTemplate uses Python's str.format method internally. If a required variable is missing, it raises a KeyError.
❓ state_output
intermediate2:00remaining
What is the output of a LangChain prompt with multiple variables?
Given the following prompt template and variable inputs, what is the final rendered string?
LangChain
from langchain.prompts import PromptTemplate prompt = PromptTemplate( input_variables=["user", "task"], template="User {user} wants to perform the task: {task}." ) output = prompt.format(user="Alice", task="data analysis")
Attempts:
2 left
💡 Hint
Check how variables are replaced in the template string.
✗ Incorrect
The format method replaces all variables with the provided values, producing the full string with substitutions.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this LangChain prompt template definition
Which option contains a syntax error that will prevent the LangChain PromptTemplate from being created?
LangChain
from langchain.prompts import PromptTemplate
Attempts:
2 left
💡 Hint
Check the type expected for input_variables parameter.
✗ Incorrect
input_variables must be a list of strings, not a single string. Passing a string causes a type error or unexpected behavior.
🔧 Debug
advanced2:00remaining
Why does this LangChain prompt fail to substitute variables correctly?
Given the code below, why does the output still contain the variable placeholders instead of the values?
LangChain
from langchain.prompts import PromptTemplate prompt = PromptTemplate( input_variables=["city"], template="Welcome to {city}!" ) output = prompt.format(city=None) print(output)
Attempts:
2 left
💡 Hint
Consider how Python's str.format handles None values.
✗ Incorrect
Passing None replaces the placeholder with the string 'None', so the output is 'Welcome to None!'.
🧠 Conceptual
expert3:00remaining
How does LangChain support dynamic content with nested variables in prompt templates?
LangChain allows prompt templates to include variables whose values are themselves templates or require runtime evaluation. Which approach correctly supports this dynamic nested content?
Attempts:
2 left
💡 Hint
Think about how LangChain composes prompts for complex workflows.
✗ Incorrect
LangChain supports nested prompt templates by allowing PromptTemplate instances as variable values, enabling dynamic content generation.