0
0
LangChainframework~20 mins

Variables and dynamic content in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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()
ARaises a KeyError because 'name' variable is missing
BOutputs 'Hello, {name}! Welcome to LangChain.' literally without substitution
COutputs 'Hello, ! Welcome to LangChain.' with empty string for missing variable
DOutputs 'Hello, None! Welcome to LangChain.' substituting None for missing variable
Attempts:
2 left
💡 Hint
Think about how Python string formatting behaves when a required key is missing.
state_output
intermediate
2: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")
A"User Alice wants to perform the task: {task}."
B"User {user} wants to perform the task: {task}."
C"User None wants to perform the task: data analysis."
D"User Alice wants to perform the task: data analysis."
Attempts:
2 left
💡 Hint
Check how variables are replaced in the template string.
📝 Syntax
advanced
2: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
APromptTemplate(input_variables=["name"], template="Hello, {name}!")
BPromptTemplate(input_variables="name", template="Hello, {name}!")
CPromptTemplate(input_variables=["name"], template='Hello, {name}!')
D)"!}eman{ ,olleH"=etalpmet ,]"eman"[=selbairav_tupni(etalpmeTtpmorP
Attempts:
2 left
💡 Hint
Check the type expected for input_variables parameter.
🔧 Debug
advanced
2: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)
ABecause None is passed, the placeholder is replaced with 'None' string
BBecause None is passed, the placeholder remains unchanged
CBecause the variable 'city' is missing, it raises a KeyError
DBecause the template string is invalid, it raises a SyntaxError
Attempts:
2 left
💡 Hint
Consider how Python's str.format handles None values.
🧠 Conceptual
expert
3: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?
AUsing PromptTemplate with input_variables and passing variables as plain strings only
BUsing string concatenation before passing variables to PromptTemplate.format()
CUsing PromptTemplate with input_variables and passing another PromptTemplate instance as a variable value
DUsing PromptTemplate with input_variables and passing a dictionary as a variable value without further formatting
Attempts:
2 left
💡 Hint
Think about how LangChain composes prompts for complex workflows.