python
type_error
ai_generated
true
TypeError: Middleware must be callable
ID: python/starlette-typeerror-middleware-must-be-callable
80%Fix Rate
86%Confidence
0Evidence
2025-09-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Passing a non-callable object (e.g., a class instance without __call__) as middleware.
generic中文
传递了一个不可调用的对象(例如没有__call__方法的类实例)作为中间件。
Workarounds
-
90% success Pass a middleware class or function
app.add_middleware(MyMiddleware)
-
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:
-
Passing a class directly instead of an instance
50% fail
Class itself is callable, but may not be the intended usage.
-
Using a string as middleware
90% fail
String is not callable.