agentscope-ai/agentscope · error · RuntimeError

{type(self).__name__} does not implement on_reasoning

Error message

{type(self).__name__} does not implement on_reasoning

What it means

Middleware.on_reasoning base stub raises RuntimeError when called, marking on_reasoning as an override-required hook for any middleware participating in the reasoning phase of the agent pipeline.

Source

Thrown at src/agentscope/middleware/_base.py:119

    async def on_reasoning(
        self,
        agent: "Agent",
        input_kwargs: dict,
        next_handler: Callable[..., AsyncGenerator],
    ) -> AsyncGenerator:
        """Hook for intercepting the reasoning process.

        Args:
            agent: The Agent instance executing this middleware
            input_kwargs: Dictionary containing:
                - tool_choice: ToolChoice (default None)
            next_handler: Callable that executes the next middleware or
            original method

        Yields:
            Various events from the reasoning process
        """
        raise RuntimeError(
            f"{type(self).__name__} does not implement on_reasoning",
        )
        yield  # pylint: disable=unreachable

    async def on_acting(
        self,
        agent: "Agent",
        input_kwargs: dict,
        next_handler: Callable[..., AsyncGenerator],
    ) -> AsyncGenerator:
        """Hook for intercepting the raw tool execution.

        This hook wraps **only** the ``toolkit.call_tool`` call — i.e. the
        pure I/O execution layer.  Permission checking, input validation, and
        context writes are handled by the agent **outside** this hook and are
        therefore not visible here.

        This separation makes it safe to offload the ``next_handler``

View on GitHub (pinned to e90f1c7592)

Solutions

  1. Implement async on_reasoning(self, agent, ..., next_handler) that yields events, delegating to next_handler for passthrough
  2. Exclude the middleware from reasoning-phase chains if it has no reasoning concern
  3. Never delegate to the base implementation

Example fix

// before
class AuditMiddleware(Middleware):
    async def on_model_call(self, agent, messages, next_handler):
        return await next_handler(messages)
# reasoning chain calls on_reasoning -> RuntimeError 234

// after
class AuditMiddleware(Middleware):
    async def on_reasoning(self, agent, reasoning, next_handler):
        async for ev in next_handler(reasoning):
            yield ev

    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_reasoning is not Middleware.on_reasoning

Type guard

def implements_on_reasoning(mw) -> bool:
    from agentscope.middleware import Middleware
    return type(mw).on_reasoning is not Middleware.on_reasoning

Prevention

When it happens

Trigger: A middleware without an on_reasoning override is included in the reasoning hook chain (agent streams reasoning events); or an override calls super().on_reasoning(...).

Common situations: Same family as 233: partial middleware implementations, template subclasses, or chains built generically that require every reasoning-phase middleware to implement the hook.

Related errors


AI-assisted analysis of agentscope-ai/agentscope@e90f1c7592 (2026-08-28). Data as JSON: /api/errors/e6cd620999e15fdb. Report an issue: GitHub.