python type_error ai_generated true

TypeError: Middleware must be callable

ID: python/starlette-typeerror-middleware-must-be-callable

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Passing a non-callable object (e.g., a class instance without __call__) as middleware.

generic

中文

传递了一个不可调用的对象(例如没有__call__方法的类实例)作为中间件。

Workarounds

  1. 90% success Pass a middleware class or function
    app.add_middleware(MyMiddleware)
  2. 85% success Ensure middleware class has __call__ method
    class MyMiddleware:
        async def __call__(self, scope, receive, send):
            # implementation
            pass

Dead Ends

Common approaches that don't work:

  1. Passing a class directly instead of an instance 50% fail

    Class itself is callable, but may not be the intended usage.

  2. Using a string as middleware 90% fail

    String is not callable.