def _serialize_context(self, obj: Any) -> Any:
        """Recursively serialize context objects to ensure JSON compatibility."""
        if isinstance(obj, bytes):
            return obj.decode("utf-8", errors="replace")
        if isinstance(obj, dict):
            return {k: self._serialize_context(v) for k, v in obj.items()}
        if isinstance(obj, (list, tuple)):
            return [self._serialize_context(x) for x in obj]
        if isinstance(obj, Exception):
            return {
                "type": obj.__class__.__name__,
                "message": str(obj),
            }
        return obj
Recursively serialize context objects to ensure JSON compatibility.
kreuzberg/exceptions.py