A programming framework for agentic AI
task: added warning when none is called in intervention handler (#4149)
Pramod Goyalcaeab68f4b57
diff --git a/python/packages/autogen-core/src/autogen_core/base/intervention.py b/python/packages/autogen-core/src/autogen_core/base/intervention.pyindex c9600ac9e..4b06fa19f 100644--- a/python/packages/autogen-core/src/autogen_core/base/intervention.py+++ b/python/packages/autogen-core/src/autogen_core/base/intervention.py@@ -1,3 +1,4 @@+import warningsfrom typing import Any, Awaitable, Callable, Protocol, finalfrom autogen_core.base import AgentId@@ -14,6 +15,23 @@ __all__ = [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]]]@@ -27,10 +45,13 @@ class InterventionHandler(Protocol):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 messageasync def on_publish(self, message: Any, *, sender: AgentId | None) -> Any | type[DropMessage]:+ _warn_if_none(message, "on_publish")return messageasync def on_response(self, message: Any, *, sender: AgentId, recipient: AgentId | None) -> Any | type[DropMessage]:+ _warn_if_none(message, "on_response")return message