{
  "id": "python/fastapi-middleware-order",
  "signature": "RuntimeError: Middleware 'CORSMiddleware' must be added before 'TrustedHostMiddleware' to work correctly.",
  "signature_zh": "运行时错误：中间件 'CORSMiddleware' 必须在 'TrustedHostMiddleware' 之前添加才能正常工作。",
  "regex": "RuntimeError:\\ Middleware\\ 'CORSMiddleware'\\ must\\ be\\ added\\ before\\ 'TrustedHostMiddleware'\\ to\\ work\\ correctly\\.",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "The order of middleware matters in FastAPI; some middleware depend on others being applied first.",
  "root_cause_type": "generic",
  "root_cause_zh": "FastAPI 中中间件的顺序很重要；某些中间件依赖于其他中间件先被应用。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Ignoring the order and adding randomly",
      "why_fails": "May cause CORS or host validation issues.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Removing one middleware to avoid the error",
      "why_fails": "Loses functionality.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add middleware in the correct order",
      "success_rate": 0.95,
      "how": "app.add_middleware(CORSMiddleware, allow_origins=['*'])\napp.add_middleware(TrustedHostMiddleware, allowed_hosts=['example.com'])",
      "condition": "",
      "sources": []
    },
    {
      "action": "Check FastAPI documentation for middleware order",
      "success_rate": 0.9,
      "how": "Consult official docs for recommended order.",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-03-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}