Challenge - 5 Problems
LangChain Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a chain step raises an exception?
Consider a LangChain chain with multiple steps. If one step raises an exception and no error handling is set, what is the behavior of the chain execution?
LangChain
from langchain.chains import SimpleSequentialChain from langchain.llms import OpenAI llm = OpenAI(temperature=0) chain1 = SimpleSequentialChain( chains=[ # First step: returns 'Hello' lambda x: 'Hello', # Second step: raises an exception lambda x: 1 / 0, # Third step: returns 'World' lambda x: 'World' ], verbose=True ) result = chain1.run('start')
Attempts:
2 left
💡 Hint
Think about what happens when Python code raises an exception without try-except blocks.
✗ Incorrect
Without explicit error handling, any exception raised in a chain step will stop the chain execution and propagate the error up. LangChain does not automatically catch or retry errors.
📝 Syntax
intermediate2:00remaining
Which code correctly adds error handling to a LangChain chain step?
You want to catch exceptions in a LangChain chain step and return a default value instead. Which code snippet correctly implements this?
LangChain
from langchain.chains import SimpleSequentialChain class SafeStep: def __init__(self, func): self.func = func def __call__(self, x): try: return self.func(x) except Exception: return 'default' chain = SimpleSequentialChain( chains=[ SafeStep(lambda x: x + ' processed'), SafeStep(lambda x: 1 / 0), # will raise SafeStep(lambda x: x + ' done') ], verbose=True ) result = chain.run('input')
Attempts:
2 left
💡 Hint
Focus on handling errors inside each step function.
✗ Incorrect
Wrapping each step in a callable class with try-except allows catching errors locally and returning a fallback value. The other options are either not supported or less precise.
❓ state_output
advanced2:00remaining
What is the output of a chain with error handling that returns fallback values?
Given the following chain with error handling wrappers, what is the final output after running with input 'start'?
LangChain
from langchain.chains import SimpleSequentialChain class SafeStep: def __init__(self, func): self.func = func def __call__(self, x): try: return self.func(x) except Exception: return 'error handled' chain = SimpleSequentialChain( chains=[ SafeStep(lambda x: x + ' step1'), SafeStep(lambda x: 1 / 0), # error triggers fallback SafeStep(lambda x: x + ' step3') ], verbose=False ) result = chain.run('start')
Attempts:
2 left
💡 Hint
Remember how SimpleSequentialChain passes output from one step to the next.
✗ Incorrect
The first step returns 'start step1'. The second step raises an exception and returns 'error handled'. The third step receives 'error handled' as input and returns 'error handled step3'. SimpleSequentialChain returns the output of the last step.
🔧 Debug
advanced2:00remaining
Why does this LangChain chain silently fail to catch errors?
This code tries to catch errors in a chain step but still crashes. What is the bug?
LangChain
from langchain.chains import SimpleSequentialChain class SafeStep: def __init__(self, func): self.func = func def __call__(self, x): try: return self.func(x) except Exception: pass # silently ignore error chain = SimpleSequentialChain( chains=[ SafeStep(lambda x: x + ' ok'), SafeStep(lambda x: 1 / 0), # error SafeStep(lambda x: x + ' done') ], verbose=True ) result = chain.run('input')
Attempts:
2 left
💡 Hint
What happens if a step returns None but the next step expects a string?
✗ Incorrect
The SafeStep catches the error but returns None implicitly (no return in except). The next step receives None and tries to concatenate, causing TypeError.
🧠 Conceptual
expert3:00remaining
How to implement retry logic for failing chain steps in LangChain?
You want to retry a chain step up to 3 times if it raises an exception before failing. Which approach correctly implements this behavior?
Attempts:
2 left
💡 Hint
LangChain does not have built-in retry parameters for chains.
✗ Incorrect
The correct way is to wrap the step function with retry logic inside a callable class. The other options either do not exist or cause uncontrolled retries or ignore errors.