{
  "id": "python/flask-working-outside-request-context",
  "signature": "RuntimeError: Working outside of request context.",
  "signature_zh": "RuntimeError: 在请求上下文之外工作。",
  "regex": "RuntimeError:\\ Working\\ outside\\ of\\ request\\ context\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Accessing request, session, or g globals from a background thread, Celery task, or CLI command without an active Flask request context.",
  "root_cause_type": "generic",
  "root_cause_zh": "在后台线程、Celery任务或CLI命令中访问request、session或g全局变量，但未激活Flask请求上下文。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "app_context does not push a request context; request, session, and g still raise RuntimeError.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This only affects exception propagation, not context availability.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "with app.test_request_context('/path', method='POST'):\\n    do_work()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "def task(user_id, payload): ...\\nuser_id = request.headers.get('X-User-Id')\\ntask.delay(user_id, request.json)",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}