Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the correct error class from LangChain.
LangChain
from langchain.chains import SimpleChain from langchain.errors import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent error class like ChainException.
✗ Incorrect
The correct error class to handle chain errors in LangChain is ChainError.
2fill in blank
mediumComplete the code to catch errors when running a chain.
LangChain
try: result = chain.run(input_data) except [1] as e: print(f"Error: {e}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching general Exception instead of ChainError.
✗ Incorrect
To specifically catch errors from LangChain chains, use ChainError.
3fill in blank
hardFix the error in the code to retry the chain on failure.
LangChain
from langchain.chains import RetryChain retry_chain = RetryChain(chain=chain, retries=[1]) result = retry_chain.run(input_data)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing retries as a string instead of an integer.
✗ Incorrect
The retries parameter expects an integer, not a string.
4fill in blank
hardFill both blanks to handle errors and log them before retrying.
LangChain
try: result = chain.run(input_data) except [1] as error: [2](f"Chain failed with error: {error}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Catching general Exception instead of ChainError.
Using print instead of logging.error.
✗ Incorrect
Catch ChainError and use logging.error to log the error message.
5fill in blank
hardFill all three blanks to create a chain with error handling and a fallback chain.
LangChain
from langchain.chains import [1], FallbackChain primary_chain = [2](llm=llm) fallback_chain = FallbackChain(chains=[primary_chain, backup_chain]) try: output = fallback_chain.run(input_text) except [3] as e: print(f"Fallback failed: {e}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SimpleChain instead of LLMChain.
Catching Exception instead of ChainError.
✗ Incorrect
Use LLMChain to create the primary chain and catch ChainError for errors.