agentscope-ai/agentscope · error · RuntimeError
f"{type(self).__name__} does not implement on_compress_conte
Error message
f"{type(self).__name__} does not implement on_compress_context" What it means
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).
Source
Thrown at src/agentscope/middleware/_base.py:260
self,
agent: "Agent",
input_kwargs: dict,
next_handler: Callable[..., Awaitable[None]],
) -> None:
"""Onion hook for `compress_context` function in `Agent` class
Args:
agent (`Agent`):
The Agent instance executing this middleware
input_kwargs (`dict`):
Dictionary containing:
- context_config: ContextConfig | None
- instructions: HintBlock | None
next_handler (`Callable[..., Awaitable[None]]`):
Callable that executes the next middleware or
original method
"""
raise RuntimeError(
f"{type(self).__name__} does not implement on_compress_context",
)
async def on_system_prompt(
self,
agent: "Agent",
current_prompt: str,
) -> str:
"""Transform the system prompt string.
This uses a transformer/pipeline pattern rather than onion pattern.
Multiple middlewares are applied sequentially, each receiving the
output of the previous one.
Args:
agent: The Agent instance executing this middleware
current_prompt: The current system prompt string
View on GitHub (pinned to e90f1c7592)
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
Example fix
// before
class MyMiddleware(Middleware):
async def on_model_call(self, agent, messages, next_handler):
return await next_handler(messages)
# context compression fires -> RuntimeError 238
// after
class MyMiddleware(Middleware):
async def on_compress_context(self, agent, context_config, instructions, next_handler):
await next_handler(context_config, instructions) # passthrough
async def on_model_call(self, agent, messages, next_handler):
return await next_handler(messages) Defensive patterns
Strategy: type-guard
Validate before calling
from agentscope.middleware import Middleware assert type(mw).on_compress_context is not Middleware.on_compress_context
Type guard
def implements_on_compress_context(mw) -> bool:
from agentscope.middleware import Middleware
return type(mw).on_compress_context is not Middleware.on_compress_context Prevention
- 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
When it happens
Trigger: 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().
Common situations: 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.
Related errors
- {type(self).__name__} does not implement on_reply
- {type(self).__name__} does not implement on_reasoning
- {type(self).__name__} does not implement on_acting
- f"{type(self).__name__} does not implement on_check_permissi
- f"{type(self).__name__} does not implement on_model_call"
AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28).
Data as JSON: /api/errors/0c99f73fb648e3c1.
Report an issue: GitHub.