0
0
LangChainframework~20 mins

ReAct agent implementation in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ReAct Agent Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this ReAct agent snippet?
Consider this simplified ReAct agent code using LangChain. What will be printed when the agent runs?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction, AgentFinish

class SimpleReActAgent(ReActAgent):
    def plan(self, intermediate_steps, **kwargs):
        if not intermediate_steps:
            return AgentAction(tool='search', tool_input='Python', log='Searching for Python')
        else:
            return AgentFinish(return_values={'output': 'Found info on Python'}, log='Done')

agent = SimpleReActAgent()
step1 = agent.plan([])
step2 = agent.plan([step1])
print(step1.log)
print(step2.log)
ADone\nSearching for Python
BSearching for Python\nDone
CSearching for Python\nSearching for Python
DDone\nDone
Attempts:
2 left
💡 Hint
Think about what the agent does when there are no intermediate steps versus when there are some.
state_output
intermediate
2:00remaining
What is the value of the agent's memory after these steps?
Given this ReAct agent code snippet, what will be the content of the agent's memory after executing the two steps?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction, AgentFinish

class MemoryReActAgent(ReActAgent):
    def __init__(self):
        super().__init__()
        self.memory = []
    def plan(self, intermediate_steps, **kwargs):
        if not intermediate_steps:
            self.memory.append('start')
            return AgentAction(tool='search', tool_input='LangChain', log='Starting search')
        else:
            self.memory.append('finish')
            return AgentFinish(return_values={'output': 'Result found'}, log='Finished')

agent = MemoryReActAgent()
agent.plan([])
agent.plan([AgentAction(tool='search', tool_input='LangChain', log='Starting search')])
memory_content = agent.memory
A[]
B['finish', 'start']
C['start']
D['start', 'finish']
Attempts:
2 left
💡 Hint
Look at when the memory list is appended to in the plan method.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this ReAct agent plan method?
Identify which plan method implementation will cause a syntax error when used in a ReAct agent subclass.
LangChain
def plan(self, intermediate_steps, **kwargs):
    # method body varies per option
A
if not intermediate_steps
    return AgentAction(tool='search', tool_input='Python', log='Searching')
else:
    return AgentFinish(return_values={'output': 'Done'}, log='Finished')
B
if not intermediate_steps:
    return AgentAction(tool='search', tool_input='Python', log='Searching')
return AgentFinish(return_values={'output': 'Done'}, log='Finished')
C
if not intermediate_steps:
    return AgentAction(tool='search', tool_input='Python', log='Searching')
else:
    return AgentFinish(return_values={'output': 'Done'}, log='Finished')
D
)'dehsiniF'=gol ,}'enoD' :'tuptuo'{=seulav_nruter(hsiniFtnegA nruter    
:esle
)'gnihcraeS'=gol ,'nohtyP'=tupni_loot ,'hcraes'=loot(noitcAtnegA nruter    
:spets_etaidemretni ton fi
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🔧 Debug
advanced
2:00remaining
Why does this ReAct agent code raise a TypeError?
This ReAct agent subclass code raises a TypeError when calling plan. What is the cause?
LangChain
from langchain.agents import ReActAgent
from langchain.schema import AgentAction

class FaultyAgent(ReActAgent):
    def plan(self, intermediate_steps, **kwargs):
        return AgentAction(tool='search', tool_input=123, log='Searching')

agent = FaultyAgent()
agent.plan([])
AAgentAction requires log to be an int, but a string was given
BMissing return statement in plan method causes TypeError
CAgentAction expects tool_input to be a string, but 123 is an int causing TypeError
Dplan method must not accept **kwargs causing TypeError
Attempts:
2 left
💡 Hint
Check the types of arguments passed to AgentAction.
🧠 Conceptual
expert
2:00remaining
Which option best describes the ReAct agent's reasoning process?
In LangChain's ReAct agent implementation, what is the core idea behind the agent's reasoning and acting cycle?
AThe agent alternates between thinking (reasoning) and acting (tool use) based on observations to solve complex tasks
BThe agent only acts once after reasoning all steps internally without intermediate observations
CThe agent randomly chooses tools without reasoning to explore the environment
DThe agent uses a fixed sequence of tool calls without adapting to intermediate results
Attempts:
2 left
💡 Hint
Consider how ReAct combines reasoning and actions iteratively.