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'?
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)
Check the length of the input string and which node the graph routes to.
The input 'Hello' has length 5, which is less than 10, so the graph routes to NodeA. The output is from NodeA's run method.
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'?
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'])
Check the length of the input and which node the graph sets in its state.
The input length is 20, which is not less than 10, so the graph sets last_node to 'NodeB'.
Identify the code snippet that will cause a syntax error when defining a LangChain graph with conditional routing.
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()
Look carefully at the syntax of the match-case statements.
The second case line is missing a colon at the end, causing a syntax error.
Given the following graph code, why does calling graph.route('test') raise a KeyError?
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')
Check when the 'node' key is added to the state dictionary.
When input is 'test', the code tries to access self.state['node'] before it is set, causing a KeyError.
Choose the most accurate description of how conditional routing works in LangChain graphs.
Think about how graphs can adapt to different inputs.
Conditional routing means the graph chooses the next node based on input or state, enabling flexible processing paths.