A programming framework for agentic AI
Fix intervention handler none check (#4351)
Jack Gerrits8347881776da
diff --git a/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py b/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.pyindex f511bd782..52d24c64d 100644--- a/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py+++ b/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py@@ -147,6 +147,23 @@ class RunContext:return self._run_state == RunContext.RunState.UNTIL_IDLE and self._runtime.idle+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,+ )++class SingleThreadedAgentRuntime(AgentRuntime):def __init__(self,@@ -433,6 +450,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):):try:temp_message = await handler.on_send(message, sender=sender, recipient=recipient)+ _warn_if_none(temp_message, "on_send")except BaseException as e:future.set_exception(e)return@@ -456,6 +474,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):):try:temp_message = await handler.on_publish(message, sender=sender)+ _warn_if_none(temp_message, "on_publish")except BaseException as e:# TODO: we should raise the intervention exception to the publisher.logger.error(f"Exception raised in in intervention handler: {e}", exc_info=True)@@ -474,6 +493,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):for handler in self._intervention_handlers:try:temp_message = await handler.on_response(message, sender=sender, recipient=recipient)+ _warn_if_none(temp_message, "on_response")except BaseException as e:# TODO: should we raise the exception to sender of the response instead?future.set_exception(e)diff --git a/python/packages/autogen-core/src/autogen_core/base/intervention.py b/python/packages/autogen-core/src/autogen_core/base/intervention.pyindex 4b06fa19f..3b771c931 100644--- a/python/packages/autogen-core/src/autogen_core/base/intervention.py+++ b/python/packages/autogen-core/src/autogen_core/base/intervention.py@@ -1,4 +1,3 @@-import warningsfrom typing import Any, Awaitable, Callable, Protocol, finalfrom autogen_core.base import AgentId@@ -15,27 +14,15 @@ __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]]]class InterventionHandler(Protocol):+ """An intervention handler is a class that can be used to modify, log or drop messages that are being processed by the :class:`autogen_core.base.AgentRuntime`.++ Note: Returning None from any of the intervention handler methods will result in a warning being issued and treated as "no change". If you intend to drop a message, you should return :class:`DropMessage` explicitly.+ """+async def on_send(self, message: Any, *, sender: AgentId | None, recipient: AgentId) -> Any | type[DropMessage]: ...async def on_publish(self, message: Any, *, sender: AgentId | None) -> Any | type[DropMessage]: ...async def on_response(@@ -44,14 +31,15 @@ class InterventionHandler(Protocol):class DefaultInterventionHandler(InterventionHandler):+ """Simple class that provides a default implementation for all intervention+ handler methods, that simply returns the message unchanged. Allows for easy+ subclassing to override only the desired methods."""+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 messagediff --git a/python/packages/autogen-core/tests/test_tools.py b/python/packages/autogen-core/tests/test_tools.pyindex 6632a7931..e79959698 100644--- a/python/packages/autogen-core/tests/test_tools.py+++ b/python/packages/autogen-core/tests/test_tools.py@@ -2,12 +2,10 @@ import inspectfrom typing import Annotated, Listimport pytest-from autogen_core.base import CancellationTokenfrom autogen_core.components._function_utils import get_typed_signaturefrom autogen_core.components.tools import BaseTool, FunctionToolfrom autogen_core.components.tools._base import ToolSchema-from pydantic import BaseModel, Field, model_serializerfrom pydantic_core import PydanticUndefined