Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(middleware): stop inheriting the previous reply's routed model (#2793)
sxh36caf32c59bd
diff --git a/src/agentscope/middleware/_model_router.py b/src/agentscope/middleware/_model_router.pyindex 3d42c554a..cd5adad20 100644--- a/src/agentscope/middleware/_model_router.py+++ b/src/agentscope/middleware/_model_router.py@@ -90,10 +90,11 @@ class ModelRouterMiddleware(MiddlewareBase):"""Swap ``agent.model`` for the selected candidate during the reply."""key = await self.get_middleware_key()original_model = agent.model- # A resumed reply keeps its route, a new one is routed on its start+ # Before ReplyStartEvent, reply_id still names the previous reply+ new_reply = isinstance(input_kwargs["inputs"], (Msg, list))routed = agent.state.middle_context.get(key, {})agent.model = self._models.get(- routed.get(agent.state.reply_id),+ None if new_reply else routed.get(agent.state.reply_id),original_model,)try:diff --git a/tests/middleware_model_router_test.py b/tests/middleware_model_router_test.pyindex 542364624..1a9366860 100644--- a/tests/middleware_model_router_test.py+++ b/tests/middleware_model_router_test.py@@ -5,7 +5,7 @@ from unittest import IsolatedAsyncioTestCasefrom pydantic import BaseModel-from utils import MockModel+from utils import AnyString, MockModelfrom agentscope.agent import Agent, InjectionConfigfrom agentscope.classifier import (ChoiceAnswer,@@ -15,6 +15,7 @@ from agentscope.classifier import ()from agentscope.credential import CredentialBasefrom agentscope.event import ModelCallStartEvent, ReplyStartEvent+from agentscope.formatter import OpenAIChatFormatterfrom agentscope.message import (Base64Source,DataBlock,@@ -221,6 +222,55 @@ class ModelRouterMiddlewareTest(IsolatedAsyncioTestCase):self.assertListEqual(active, [self.reasoning, self.primary])self.assertIs(agent.model, self.primary)+ async def test_new_reply_is_gated_by_its_own_routing(self) -> None:+ """A new reply's media is not gated by the previous reply's route."""+ # The fast model accepts no media, so a stale route drops the audio+ self.fast.formatter = OpenAIChatFormatter(input_types=["text/plain"])+ for model in (self.primary, self.fast, self.reasoning):+ model.context_size = 100000+ middleware = ModelRouterMiddleware(+ _MockClassifier(["fast", "reasoning"]),+ self.candidates,+ )++ agent, called = await self._reply(+ middleware,+ [TextBlock(text="Say hello.")],+ [+ TextBlock(text="What is in this recording?"),+ DataBlock(+ source=Base64Source(data="AA==", media_type="audio/wav"),+ ),+ ],+ )++ self.assertListEqual(called, ["fast-model", "reasoning-model"])+ user_msg = [_ for _ in agent.state.context if _.role == "user"][-1]+ self.assertListEqual(+ [_.model_dump() for _ in user_msg.content],+ [+ {+ "type": "text",+ "text": "What is in this recording?",+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ {+ "type": "data",+ "id": AnyString(),+ "source": {+ "type": "base64",+ "data": "AA==",+ "media_type": "audio/wav",+ },+ "name": None,+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ )+async def test_chat_model_routes_with_structured_output(self) -> None:"""A chat model routes through a structured choice."""routing_model = _MockRoutingChatModel("fast")