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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
32 changes: 28 additions & 4 deletions python/packages/autogen-core/src/autogen_core/base/intervention.py
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,27 @@
class DropMessage: ...


def warn_if_none(result: Any, handler_name: str) -> Any:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name this with a leading underscore to indicate private

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply, I went on a weekend trip. Done.

jackgerrits marked this conversation as resolved.
Show resolved Hide resolved
"""
Utility function to check if the intervention handler returned None and issue a warning.

Args:
result: The return value from the intervention handler
handler_name: Name of the intervention handler method for the warning message

Returns:
The original result value
"""
if result 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
)
return result


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


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

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

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

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