0
0
LangChainframework~20 mins

Conditional routing in graphs in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Conditional Routing Master
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 LangChain graph with conditional routing?

Consider a LangChain graph where the routing depends on the input text length. The graph routes to NodeA if the input length is less than 10, otherwise to NodeB. What will be the output if the input is 'Hello'?

LangChain
from langchain.graphs import Graph
from langchain.chains import LLMChain

class NodeA(LLMChain):
    def run(self, input_text):
        return f"Short input processed: {input_text}"

class NodeB(LLMChain):
    def run(self, input_text):
        return f"Long input processed: {input_text}"

class ConditionalGraph(Graph):
    def route(self, input_text):
        if len(input_text) < 10:
            return NodeA()
        else:
            return NodeB()

graph = ConditionalGraph()
result = graph.route('Hello').run('Hello')
print(result)
ATypeError at runtime
B"Long input processed: Hello"
C"Short input processed: Hello"
DSyntaxError at runtime
Attempts:
2 left
💡 Hint

Check the length of the input string and which node the graph routes to.

state_output
intermediate
2:00remaining
What is the state of the graph after routing with input 'This is a test input'?

Given a LangChain graph that stores the last routed node name in its state, what will be the value of graph.state['last_node'] after routing the input 'This is a test input'?

LangChain
from langchain.graphs import Graph

class StatefulGraph(Graph):
    def __init__(self):
        super().__init__()
        self.state = {}

    def route(self, input_text):
        if len(input_text) < 10:
            self.state['last_node'] = 'NodeA'
            return 'NodeA'
        else:
            self.state['last_node'] = 'NodeB'
            return 'NodeB'

graph = StatefulGraph()
graph.route('This is a test input')
print(graph.state['last_node'])
A"NodeB"
BNone
C"NodeA"
DKeyError: 'last_node'
Attempts:
2 left
💡 Hint

Check the length of the input and which node the graph sets in its state.

📝 Syntax
advanced
2:00remaining
Which option will cause a syntax error in defining a conditional routing graph?

Identify the code snippet that will cause a syntax error when defining a LangChain graph with conditional routing.

LangChain
from langchain.graphs import Graph

class MyGraph(Graph):
    def route(self, input_text):
        match input_text:
            case str() if len(input_text) < 5:
                return 'ShortNode'
            case str() if len(input_text) >= 5:
                return 'LongNode'

graph = MyGraph()
AMissing colon after 'case str() if len(input_text) >= 5' causes SyntaxError
BUsing 'match' without 'case' causes SyntaxError
CIndentation error in 'return' statement causes SyntaxError
DNo syntax error in the code
Attempts:
2 left
💡 Hint

Look carefully at the syntax of the match-case statements.

🔧 Debug
advanced
2:00remaining
Why does this LangChain graph raise a KeyError during routing?

Given the following graph code, why does calling graph.route('test') raise a KeyError?

LangChain
from langchain.graphs import Graph

class DebugGraph(Graph):
    def __init__(self):
        super().__init__()
        self.state = {}

    def route(self, input_text):
        if input_text == 'test':
            return self.state['node']
        else:
            self.state['node'] = 'NodeX'
            return 'NodeX'

graph = DebugGraph()
graph.route('test')
AThe 'state' dictionary is not initialized, causing AttributeError
BThe 'input_text' variable is undefined causing NameError
CThe 'route' method returns None causing TypeError
DThe 'node' key is not set in state before access, causing KeyError
Attempts:
2 left
💡 Hint

Check when the 'node' key is added to the state dictionary.

🧠 Conceptual
expert
2:00remaining
Which option best describes conditional routing in LangChain graphs?

Choose the most accurate description of how conditional routing works in LangChain graphs.

ARouting is fixed at graph creation and cannot change based on input or state.
BRouting decisions are made dynamically based on input data or state, allowing different nodes to process different inputs.
CRouting depends only on the order nodes were added to the graph, ignoring input content.
DRouting is random and does not consider input or state.
Attempts:
2 left
💡 Hint

Think about how graphs can adapt to different inputs.