from typing import TypeVar, Callable, Optional
O = TypeVar("O")
T = TypeVar("T")
def safe_get(obj: O, accessor: Callable[[O], T], default: Optional[T] = None) -> Optional[T]:
"""
Safely access a nested attribute or mapping value of a generic object, avoiding AttributeError.
Args:
obj: The object of type O to access.
accessor: A lambda function taking O and returning T, e.g.,
lambda c: c.scenarios.deep_research.final_report_model
default: Value to return if access fails.
Returns:
The value of type T if access succeeds, else default.
"""
try:
return accessor(obj)
except AttributeError:
return default
except Exception:
return default