Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: added warning when none is called in intervention handler #4149

Merged
merged 9 commits into from
Nov 23, 2024
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from typing import Any, Awaitable, Callable, Protocol, final

from autogen_core.base import AgentId

__all__ = [
Expand All @@ -14,6 +14,23 @@
class DropMessage: ...


def _warn_if_none(value: Any, handler_name: str) -> None:
"""
Utility function to check if the intervention handler returned None and issue a warning.

Args:
value: The return value to check
handler_name: Name of the intervention handler method for the warning message
"""
if value is None:
warnings.warn(
f"Intervention handler {handler_name} returned None. This might be unintentional. "
"Consider returning the original message or DropMessage explicitly.",
RuntimeWarning,
stacklevel=2
)


InterventionFunction = Callable[[Any], Any | Awaitable[type[DropMessage]]]


Expand All @@ -27,10 +44,13 @@ async def on_response(

class DefaultInterventionHandler(InterventionHandler):
async def on_send(self, message: Any, *, sender: AgentId | None, recipient: AgentId) -> Any | type[DropMessage]:
_warn_if_none(message, "on_send")
return message

async def on_publish(self, message: Any, *, sender: AgentId | None) -> Any | type[DropMessage]:
_warn_if_none(message, "on_publish")
return message

async def on_response(self, message: Any, *, sender: AgentId, recipient: AgentId | None) -> Any | type[DropMessage]:
_warn_if_none(message, "on_response")
return message
Loading