{
  "id": "python/starlette-middleware-order-500",
  "signature": "RuntimeError: Cannot add middleware after an application has started",
  "signature_zh": "RuntimeError: 应用启动后无法再添加中间件",
  "regex": "RuntimeError:\\ Cannot\\ add\\ middleware\\ after\\ an\\ application\\ has\\ started",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "add_middleware is called after the ASGI app has begun handling requests, e.g. inside a startup event or a route.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 ASGI 应用开始处理请求后（例如在 startup 事件或路由中）调用了 add_middleware。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Startup runs after the middleware stack is built; the error is raised.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The middleware is never applied, so behavior silently differs.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "Register middleware at module level before the app starts:\napp.add_middleware(GZipMiddleware)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Use lifespan for startup logic, not middleware registration.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}