{
  "id": "python/asyncio-await-outside-async-function",
  "signature": "SyntaxError: 'await' outside async function",
  "signature_zh": "SyntaxError: 'await' 出现在 async 函数之外",
  "regex": "SyntaxError:\\ 'await'\\ outside\\ async\\ function",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The await keyword was used in a regular def function or at module top level. await is only valid inside async def bodies (or in top-level await when using IPython/Jupyter).",
  "root_cause_type": "generic",
  "root_cause_zh": "await 关键字被用在普通的 def 函数或模块顶层。await 只能在 async def 函数体内使用（或在 IPython/Jupyter 的顶层 await 中）。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "If the sync function is itself called from async code, this raises 'This event loop is already running'.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Coroutine objects have no result() method; raises AttributeError.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "async def handler():\n    data = await fetch()\n    return data",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "async def main():\n    await fetch()\nif __name__ == '__main__':\n    asyncio.run(main())",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# IPython/Jupyter supports top-level await in cells\nawait fetch()",
      "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.92,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}