{"record":{"id":"0c99f73fb648e3c1","repo":"agentscope-ai/agentscope","slug":"f-type-self-name-does-not-implement-on-comp","errorCode":null,"errorMessage":"f\"{type(self).__name__} does not implement on_compress_context\"","messagePattern":"f\"(.+?) does not implement on_compress_context\"","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/agentscope/middleware/_base.py","lineNumber":260,"sourceCode":"        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\n            next_handler (`Callable[..., Awaitable[None]]`):\n                Callable that executes the next middleware or\n                original method\n        \"\"\"\n        raise RuntimeError(\n            f\"{type(self).__name__} does not implement on_compress_context\",\n        )\n\n    async def on_system_prompt(\n        self,\n        agent: \"Agent\",\n        current_prompt: str,\n    ) -> str:\n        \"\"\"Transform the system prompt string.\n\n        This uses a transformer/pipeline pattern rather than onion pattern.\n        Multiple middlewares are applied sequentially, each receiving the\n        output of the previous one.\n\n        Args:\n            agent: The Agent instance executing this middleware\n            current_prompt: The current system prompt string\n","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/middleware/_base.py#L242-L278","documentation":"Middleware.on_compress_context base stub raises RuntimeError when the context-compression hook fires on a middleware that did not implement it. Middlewares participating in context compression must override on_compress_context to transform or forward the compression request (context_config, instructions).","triggerScenarios":"A middleware lacking on_compress_context is included in the chain when the agent triggers context compression (long conversations exceeding context limits); or an override calls super().","commonSituations":"Enabling context compression on an agent whose custom middlewares were written before the compression hook existed; adding unrelated middlewares to the default chain which the compression pass then traverses.","solutions":["Implement async on_compress_context(self, agent, context_config, instructions, next_handler) delegating via next_handler if no transformation is needed","Ensure only compression-aware middlewares are in the compression chain","Upgrade/downgrade-check middleware code against the version that introduced the hook"],"exampleFix":"// before\nclass MyMiddleware(Middleware):\n    async def on_model_call(self, agent, messages, next_handler):\n        return await next_handler(messages)\n# context compression fires -> RuntimeError 238\n\n// after\nclass MyMiddleware(Middleware):\n    async def on_compress_context(self, agent, context_config, instructions, next_handler):\n        await next_handler(context_config, instructions)  # passthrough\n\n    async def on_model_call(self, agent, messages, next_handler):\n        return await next_handler(messages)","handlingStrategy":"type-guard","validationCode":"from agentscope.middleware import Middleware\nassert type(mw).on_compress_context is not Middleware.on_compress_context","typeGuard":"def implements_on_compress_context(mw) -> bool:\n    from agentscope.middleware import Middleware\n    return type(mw).on_compress_context is not Middleware.on_compress_context","tryCatchPattern":null,"preventionTips":["If you enable context compression, ensure all middlewares implement the hook","Test long-conversation scenarios that trigger compression","Ship a passthrough on_compress_context in your middleware base class"],"tags":["middleware","context-compression","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"}