Always verify that keys, IDs, indices, or other required values exist before performing operations that depend on them. This prevents runtime errors and unexpected behavior from null references or missing data.

Key patterns to follow:

Example of the pattern:

# Before: Potential KeyError or duplicate registration
REGISTERED_FUNCTIONS[key] = func

# After: Check existence first
if key in REGISTERED_FUNCTIONS:
    raise ValueError(f"Function {key} already registered")
REGISTERED_FUNCTIONS[key] = func

# Before: Potential IndexError
summary_text_chunk = summary[0]["text"]

# After: Check bounds and existence
if summary and len(summary) > 0:
    summary_text_chunk = summary[0]["text"]

This proactive approach prevents silent failures and makes code more robust by catching potential issues at the point of access rather than allowing them to propagate.