0
0
Agentic AIml~5 mins

Handling retrieval failures gracefully in Agentic AI

Choose your learning style9 modes available
Introduction

Sometimes, when an AI tries to get information, it might fail. Handling these failures nicely helps the AI keep working without crashing or giving wrong answers.

When an AI agent asks a database for information but the data is missing.
When a chatbot tries to fetch user details but the connection is lost.
When a recommendation system cannot find matching items for a user.
When an AI assistant calls an external API that is temporarily down.
When a search engine returns no results for a query.
Syntax
Agentic AI
class RetrievalHandler:
    def __init__(self, data_source):
        self.data_source = data_source

    def get_data(self, key):
        try:
            result = self.data_source.retrieve(key)
            if result is None:
                return self.handle_failure(key)
            return result
        except Exception as error:
            return self.handle_failure(key, error)

    def handle_failure(self, key, error=None):
        if error:
            print(f"Error retrieving {key}: {error}")
        else:
            print(f"No data found for {key}")
        # Return a safe default or message
        return "Data not available"

This class tries to get data and catches errors or missing data.

The handle_failure method manages what happens when retrieval fails.

Examples
Normal case: data exists and is returned.
Agentic AI
class DummyDataSource:
    def retrieve(self, key):
        data = {"name": "Alice", "age": 30}
        return data.get(key)

handler = RetrievalHandler(DummyDataSource())
print(handler.get_data("name"))  # Returns 'Alice'
Missing key: returns 'Data not available' and prints a message.
Agentic AI
print(handler.get_data("address"))  # Key missing, triggers graceful failure
Error case: retrieval raises exception, handled without crash.
Agentic AI
class ErrorDataSource:
    def retrieve(self, key):
        raise RuntimeError("Connection lost")

error_handler = RetrievalHandler(ErrorDataSource())
print(error_handler.get_data("name"))  # Handles exception gracefully
Sample Model

This program shows how to handle missing data and errors when retrieving information from different sources.

Agentic AI
class RetrievalHandler:
    def __init__(self, data_source):
        self.data_source = data_source

    def get_data(self, key):
        try:
            result = self.data_source.retrieve(key)
            if result is None:
                return self.handle_failure(key)
            return result
        except Exception as error:
            return self.handle_failure(key, error)

    def handle_failure(self, key, error=None):
        if error:
            print(f"Error retrieving {key}: {error}")
        else:
            print(f"No data found for {key}")
        return "Data not available"

class DummyDataSource:
    def __init__(self):
        self.data = {"name": "Alice", "age": 30}

    def retrieve(self, key):
        return self.data.get(key)

class ErrorDataSource:
    def retrieve(self, key):
        raise RuntimeError("Connection lost")

print("-- Using DummyDataSource --")
dummy_handler = RetrievalHandler(DummyDataSource())
print("Name:", dummy_handler.get_data("name"))
print("Address:", dummy_handler.get_data("address"))

print("\n-- Using ErrorDataSource --")
error_handler = RetrievalHandler(ErrorDataSource())
print("Name:", error_handler.get_data("name"))
OutputSuccess
Important Notes

Time complexity depends on the data source retrieval method, usually O(1) for dictionary lookups.

Space complexity is minimal, just storing references to data sources and keys.

Common mistake: not catching exceptions, which can crash the program.

Use graceful failure handling to keep AI systems stable and user-friendly.

Summary

Always prepare for missing or error data when retrieving information.

Use try-except blocks and check for None to handle failures.

Return safe default values to keep your AI working smoothly.