0
0
LangChainframework~20 mins

LangChain vs direct API calls - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the main difference in output when using LangChain's chain vs direct API call?

Consider a LangChain chain that calls an LLM with a prompt template versus a direct API call to the same LLM with the same prompt. What difference in output behavior would you expect?

LangChain
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"])
chain = LLMChain(llm=llm, prompt=prompt)

result_chain = chain.run(text="Hello")

# Direct API call
result_direct = llm("Translate 'Hello' to French.")
ALangChain output is a Python dict, direct API call returns a string.
BLangChain output includes extra metadata, while direct API call returns only the raw text.
CLangChain output is always JSON, direct API call returns plain text.
DBoth outputs are identical strings because LangChain just wraps the API call with formatting.
Attempts:
2 left
💡 Hint

Think about what LangChain adds on top of the API call.

state_output
intermediate
2:00remaining
What happens to state when using LangChain chains vs direct API calls?

When you run multiple prompts in sequence, how does LangChain manage state compared to direct API calls?

LangChain
from langchain import ConversationChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
conv = ConversationChain(llm=llm)

response1 = conv.predict(input="Hello!")
response2 = conv.predict(input="How are you?")

# Direct calls
resp1 = llm("Hello!")
resp2 = llm("How are you?")
ADirect API calls keep conversation history; LangChain chains do not.
BBoth LangChain and direct API calls keep conversation history automatically.
CLangChain ConversationChain keeps conversation history automatically; direct API calls do not.
DNeither LangChain nor direct API calls keep conversation history automatically.
Attempts:
2 left
💡 Hint

Think about how conversation context is preserved.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates a LangChain chain that calls OpenAI with a prompt template?

Identify the correct code to create a LangChain LLMChain with a prompt template that asks for a summary.

A
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

prompt = PromptTemplate(template="Summarize: {text}", input_variables=["text"])
llm = OpenAI()
chain = LLMChain(llm=llm, prompt=prompt)
B
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

prompt = PromptTemplate(template="Summarize: {text}", input_variables=["text"])
llm = OpenAI()
chain = LLMChain(prompt, llm)
C
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

prompt = PromptTemplate(template="Summarize: {text}", input_variables=["text"])
llm = OpenAI()
chain = LLMChain(llm=llm)
D
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

prompt = PromptTemplate(template="Summarize: {text}", input_variables=["text"])
llm = OpenAI()
chain = LLMChain(prompt=prompt)
Attempts:
2 left
💡 Hint

Check the order and presence of required arguments in LLMChain constructor.

🔧 Debug
advanced
2:00remaining
Why does this LangChain code raise a KeyError when running a chain?

Given this code, why does running chain.run() raise a KeyError?

LangChain
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

prompt = PromptTemplate(template="Answer: {question}", input_variables=["question"])
llm = OpenAI()
chain = LLMChain(llm=llm, prompt=prompt)

result = chain.run()
ABecause OpenAI LLM is not initialized with an API key.
BBecause <code>run()</code> requires the 'question' argument but none was provided.
CBecause <code>LLMChain</code> does not support <code>run()</code> method.
DBecause the prompt template is invalid and missing a colon.
Attempts:
2 left
💡 Hint

Check the input variables required by the prompt template.

🧠 Conceptual
expert
2:00remaining
Which advantage does LangChain provide over direct API calls for building complex applications?

Choose the best advantage LangChain offers compared to making direct API calls to language models.

ALangChain automatically manages prompt templates, chains, memory, and integrations, simplifying complex workflows.
BLangChain reduces API latency by caching all responses locally.
CLangChain replaces the need for API keys by using open-source models only.
DLangChain guarantees 100% accurate outputs from language models.
Attempts:
2 left
💡 Hint

Think about what LangChain adds beyond just calling the API.