{"record":{"id":"66ba181a8c192693","repo":"agentscope-ai/agentscope","slug":"f-type-self-name-does-not-implement-on-mode","errorCode":null,"errorMessage":"f\"{type(self).__name__} does not implement on_model_call\"","messagePattern":"f\"(.+?) does not implement on_model_call\"","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/middleware/_base.py","lineNumber":237,"sourceCode":"            Awaitable[\"ChatResponse\" | AsyncGenerator[\"ChatResponse\", None]],\n        ],\n    ) -> \"ChatResponse\" | AsyncGenerator[\"ChatResponse\", None]:\n        \"\"\"Hook for intercepting the model API call.\n\n        Args:\n            agent: The Agent instance executing this middleware\n            input_kwargs: Dictionary containing:\n                - messages: list[Msg]\n                - tools: list[dict]\n                - tool_choice: ToolChoice\n                - current_model: The model instance used for this call\n            next_handler: Callable that executes the next middleware or\n            original method\n\n        Returns:\n            ChatResponse or AsyncGenerator[ChatResponse, None]\n        \"\"\"\n        raise RuntimeError(\n            f\"{type(self).__name__} does not implement on_model_call\",\n        )\n\n    async def on_compress_context(\n        self,\n        agent: \"Agent\",\n        input_kwargs: dict,\n        next_handler: Callable[..., Awaitable[None]],\n    ) -> None:\n        \"\"\"Onion hook for `compress_context` function in `Agent` class\n\n        Args:\n            agent (`Agent`):\n                The Agent instance executing this middleware\n            input_kwargs (`dict`):\n                Dictionary containing:\n                - context_config: ContextConfig | None\n                - instructions: HintBlock | None","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/middleware/_base.py#L219-L255","documentation":"Middleware.on_model_call base stub raises RuntimeError when execute_chain dispatches a model call to a middleware that did not override on_model_call. Any middleware in the model-call chain must implement this hook to intercept or forward ChatResponse generation.","triggerScenarios":"A middleware without on_model_call is placed in the model-call chain; or an override calls super().on_model_call(...). Raised on the agent's first LLM invocation with that chain.","commonSituations":"Writing a context-compression or logging middleware and forgetting the model-call passthrough; version upgrades changing the hook from optional to required in the chain; copy-paste middleware templates missing the method.","solutions":["Implement async on_model_call(self, agent, messages, next_handler) returning/forwarding next_handler(messages)","Remove the middleware from model-call chains if it doesn't need to intercept them","Match the signature to the installed agentscope version"],"exampleFix":"// before\nclass CacheMiddleware(Middleware):\n    async def on_reply(self, agent, reply, next_handler):\n        async for ev in next_handler(reply):\n            yield ev\n# model call chain -> RuntimeError 237\n\n// after\nclass CacheMiddleware(Middleware):\n    async def on_model_call(self, agent, messages, next_handler):\n        key = hash(str(messages))\n        if key in cache:\n            return cache[key]\n        resp = await next_handler(messages)\n        cache[key] = resp\n        return resp","handlingStrategy":"type-guard","validationCode":"from agentscope.middleware import Middleware\nassert type(mw).on_model_call is not Middleware.on_model_call","typeGuard":"def implements_on_model_call(mw) -> bool:\n    from agentscope.middleware import Middleware\n    return type(mw).on_model_call is not Middleware.on_model_call","tryCatchPattern":null,"preventionTips":["Give every middleware a default on_model_call that forwards next_handler","Test middlewares with one end-to-end agent call before shipping","Update middleware subclasses when upgrading agentscope versions"],"tags":["middleware","model-call","hook-not-implemented"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}