Skip to content

Commit

Permalink
task: added warning when none is called in intervention handler (#4149)
Browse files Browse the repository at this point in the history
* task: added warning when none is called in intervention handler

* add leading underscore to indicate private to _warn_if_none method in intervention.py

* address comment of returning Any for result in intervention.py

* Update intervention.py to remove redundant name change

* Format and lint

---------

Co-authored-by: Jack Gerrits <[email protected]>
Co-authored-by: Jack Gerrits <[email protected]>
  • Loading branch information
3 people authored Nov 23, 2024
1 parent 0b5eaf1 commit caeab68
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/packages/autogen-core/src/autogen_core/base/intervention.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Any, Awaitable, Callable, Protocol, final

from autogen_core.base import AgentId
Expand All @@ -14,6 +15,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 +45,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

0 comments on commit caeab68

Please sign in to comment.