Bird
Raised Fist0
Agentic AIml~20 mins

Handling retrieval failures gracefully in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Retrieval Resilience Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is it important to handle retrieval failures gracefully in AI systems?

Imagine an AI assistant that fetches information from a database. What is the main reason to handle retrieval failures gracefully?

ATo provide a smooth user experience even when data is missing or delayed
BTo ensure the system crashes quickly so developers notice the problem
CTo ignore errors and continue without informing the user
DTo always return empty results without explanation
Attempts:
2 left
💡 Hint

Think about how users feel when an app suddenly stops working or shows confusing errors.

Predict Output
intermediate
1:30remaining
What is the output when retrieval fails and fallback is used?

Consider this Python code snippet simulating a retrieval with fallback:

def retrieve_data(key):
    data_store = {'a': 1, 'b': 2}
    try:
        return data_store[key]
    except KeyError:
        return 'default_value'

result = retrieve_data('c')
print(result)

What will be printed?

A'default_value'
B2
CKeyError
D1
Attempts:
2 left
💡 Hint

What happens if the key is not found in the dictionary?

Model Choice
advanced
2:00remaining
Which model architecture is best suited to handle missing data during retrieval?

You want an AI model that can still make reasonable predictions even if some input data is missing or incomplete. Which model type is best?

AStandard linear regression without imputation
BFeedforward neural network without dropout or masking
CRecurrent neural network with masking for missing inputs
DDecision tree that requires complete data
Attempts:
2 left
💡 Hint

Think about models that can ignore or skip missing parts of the input.

Metrics
advanced
2:00remaining
Which metric best reflects graceful handling of retrieval failures in AI predictions?

An AI system sometimes returns fallback predictions when retrieval fails. Which metric helps measure if these fallbacks keep predictions reliable?

AAccuracy on only complete data samples
BMean squared error including fallback predictions
CTraining loss before deployment
DNumber of retrieval failures logged
Attempts:
2 left
💡 Hint

Consider a metric that measures prediction quality including fallback cases.

🔧 Debug
expert
2:30remaining
Why does this retrieval fallback code cause a runtime error?

Review this Python code snippet:

def get_data(key, fallback=None):
    data = {'x': 10, 'y': 20}
    try:
        return data[key]
    except KeyError:
        return fallback.upper()

result = get_data('z', fallback=None)
print(result)

What error occurs and why?

ATypeError because data[key] returns an int but fallback is None
BNo error, prints None
CKeyError because 'z' is not in data and fallback is ignored
DAttributeError because fallback is None and None has no 'upper' method
Attempts:
2 left
💡 Hint

What happens if fallback is None and you call a string method on it?

Practice

(1/5)
1. Why is it important to handle retrieval failures gracefully in agentic AI systems?
easy
A. To keep the AI running smoothly without crashing
B. To make the AI run faster
C. To increase the size of the data retrieved
D. To avoid using any default values

Solution

  1. Step 1: Understand retrieval failures

    Retrieval failures happen when the AI cannot get the needed data, which can cause errors.
  2. Step 2: Importance of graceful handling

    Handling failures gracefully means preventing crashes and keeping the AI working by managing errors properly.
  3. Final Answer:

    To keep the AI running smoothly without crashing -> Option A
  4. Quick Check:

    Graceful failure handling = prevent crashes [OK]
Hint: Think about avoiding crashes by handling errors safely [OK]
Common Mistakes:
  • Assuming failures speed up the AI
  • Ignoring the need for default values
  • Believing more data is always retrieved
2. Which Python syntax correctly handles a retrieval failure using try-except?
easy
A. try: data = retrieve_info() except Exception: data = None
B. if data == None: retrieve_info() else: pass
C. try: data = retrieve_info() finally: data = None
D. data = retrieve_info() if data else None

Solution

  1. Step 1: Identify try-except usage

    try: data = retrieve_info() except Exception: data = None uses try-except to catch errors during retrieval and sets data to None if an error occurs.
  2. Step 2: Check other options for correctness

    Options A, B, and C misuse syntax or logic for error handling.
  3. Final Answer:

    try: data = retrieve_info() except Exception: data = None -> Option A
  4. Quick Check:

    try-except for errors = try: data = retrieve_info() except Exception: data = None [OK]
Hint: Look for try-except blocks catching exceptions [OK]
Common Mistakes:
  • Using if without try-except for errors
  • Misusing finally block to handle errors
  • Incorrect conditional expressions
3. What will be the output of this code snippet?
def get_data():
    try:
        return None
    except:
        return 'Error'

result = get_data() or 'Default'
print(result)
medium
A. None
B. Default
C. Error
D. Exception

Solution

  1. Step 1: Analyze get_data function

    The function returns None without raising an exception, so except block is skipped.
  2. Step 2: Evaluate result assignment

    Since get_data() returns None (which is falsey), the expression uses 'Default' instead.
  3. Final Answer:

    Default -> Option B
  4. Quick Check:

    None or 'Default' = 'Default' [OK]
Hint: Remember None is falsey, so 'or' picks the default [OK]
Common Mistakes:
  • Thinking None prints as 'None' string
  • Assuming except block runs without error
  • Confusing return values with exceptions
4. Identify the error in this code that tries to handle retrieval failure:
def fetch_data():
    try:
        data = retrieve()
    except:
        data = None
    return data

result = fetch_data()
print(result)
medium
A. Data variable is not defined
B. Missing parentheses in retrieve call
C. No return statement in function
D. No specific exception caught in except block

Solution

  1. Step 1: Check function structure

    The function calls retrieve() correctly and returns data, so no missing parentheses or return issues.
  2. Step 2: Analyze except block

    The except block catches all exceptions without specifying which, which is bad practice and can hide bugs.
  3. Final Answer:

    No specific exception caught in except block -> Option D
  4. Quick Check:

    Use specific exceptions, not bare except [OK]
Hint: Avoid bare except; specify exceptions to catch [OK]
Common Mistakes:
  • Thinking missing parentheses cause error
  • Ignoring importance of specific exceptions
  • Assuming data is undefined
5. You want your AI agent to retrieve user info but return a safe default if retrieval fails. Which approach is best?
def get_user_info(user_id):
    try:
        info = retrieve_user(user_id)
        if info is None:
            return {'name': 'Guest', 'id': 0}
        return info
    except RetrievalError:
        return {'name': 'Guest', 'id': 0}
hard
A. Return None on failure and handle later
B. Raise error immediately without handling
C. Use try-except and return a default dict on failure or missing data
D. Return empty string on failure

Solution

  1. Step 1: Understand retrieval and failure cases

    The function tries to get user info, checks if data is missing (None), and handles exceptions.
  2. Step 2: Evaluate handling strategy

    Returning a default dictionary for missing or failed retrieval keeps AI stable and predictable.
  3. Final Answer:

    Use try-except and return a default dict on failure or missing data -> Option C
  4. Quick Check:

    Safe defaults on failure = Use try-except and return a default dict on failure or missing data [OK]
Hint: Return safe defaults inside try-except for smooth AI [OK]
Common Mistakes:
  • Returning None and not handling later
  • Raising errors without fallback
  • Returning empty strings instead of structured defaults